#485

Max Consecutive Ones

Easy Running counter

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

In a binary array, return the length of the longest run of consecutive 1s.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the longest unbroken streak of 1s.

Why this works: one counter grows on 1 and resets on 0; remember the best value it ever reached.

Example: [1, 1, 0, 1, 1, 1]

run: 1, 2, 0, 1, 2, 3best = 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Updating the best only when a 0 appears, which misses a run that ends at the last element.