Max Consecutive Ones

Easy~10 min

Given a binary array nums (containing only 0s and 1s), return the maximum number of consecutive 1s in the array.

For example, given [1, 1, 0, 1, 1, 1], the longest streak of consecutive 1s is 3 (the last three elements).

Examples

Example 1
Input: nums = [1, 1, 0, 1, 1, 1]
Output: 3
Explanation: The longest run of 1s is the last three elements: [1, 1, 1].
Example 2
Input: nums = [1, 0, 1, 1, 0, 1]
Output: 2
Explanation: There are two runs of consecutive 1s with length 2: indices 2-3 and the single 1 at the end.
Example 3
Input: nums = [0, 0, 0]
Output: 0
Explanation: No 1s in the array, so the maximum consecutive count is 0.

Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run