Max Consecutive Ones
Easy · sliding window
A list nums holds only the numbers 0 and 1. Think of it as a row of lamps, where 1 is a lamp that is on and 0 is a lamp that is off.
Return the length of the longest stretch of neighbouring lamps that are all on. If every lamp is off, return 0.
Examples
Example 1
Input: nums = [1, 1, 0, 1, 1, 1]
Output: 3
The stretch at the end has three lamps on. The one at the start has only two.
Example 2
Input: nums = [1, 0, 1, 0, 1]
Output: 1
The lamps that are on are never next to each other, so the best stretch is a single lamp.
Example 3
Input: nums = [0, 0]
Output: 0
Constraints
- 1 <= len(nums) <= 10^5
- nums[i] is 0 or 1