LeetCode #485 Max Consecutive Ones
Problem
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:
- The input array will only contain
0
and1
. - The length of input array is a positive integer and will not exceed 10,000
Solution
Iterate list once and count if pointer meets 1
. And compare current count to saved largest count and update largest if needed.
Complexity
List is iterated only once so it takes O(n) time if n
denotes to length of given list nums
.
And it only uses O(1) extra space.