Max Consecutive Ones
Given a binary array nums (an array containing only 0s and 1s), return the maximum number of consecutive 1's in the array.
[ 1, 1, 0, 1, 1, 1 ]
Explanation. The first two 1's form the first consecutive group, and the last three 1's form the longest consecutive group, which counts to three.
[ 1, 0, 1, 0, 1, 0, 1 ]
Explanation. There are several groups of consecutive 1's, but each group only contains one 1.
[ 0, 0, 0, 0 ]
Explanation. There are no 1's in the array, so the maximum number of consecutive 1's is zero.
[ 1, 1, 1, 1, 1 ]
Explanation. All elements in the array are 1's, making the entire array one large consecutive group of 1's.
Follow-up: How would you handle the situation if the input is coming from a stream of data?
1. The array `nums` must contain at least 1 element. 2. All elements in `nums` are either 0 or 1.
- Views
- 2