Max Consecutive Ones

Given a binary array nums, return the length of the longest run of consecutive 1s in it.

30:00

Max Consecutive Ones
easy
Free written solution
Topics
Companies

Given a binary array nums, return the length of the longest run of consecutive 1s in it.

Example 1:
Input: {"nums":[1,1,0,1,1,1]}
Output: 3
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • nums[i]\text{nums}[i] is either 00 or 11.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,1,0,1,1,1]}

Scan 6 bits once, tracking the live streak (cur) and the record (best).

nums (6 bits)

▬ best streak

▬ current streak

1

0

1

1

0

2

1

3

1

4

1

5

cur

0

best

0

Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/8