Longest Run of Consecutive Ones

Implement longestRunOfOnes

Given a binary array nums containing only 0s and 1s, return the length of the longest run of consecutive 1s in the array. Checking every starting index and walking forward works, but it re-derives information a single pass already has: whether the current streak is still alive. The sliding windowSliding WindowMaintaining a running result over a contiguous range that grows or shrinks one element at a time, instead of recomputing the result for every range from scratch. idea shows up here in its simplest form — a streak counter that grows by one on every 1 and collapses to 0 on every 0, with the best value seen along the way kept as the answer.

Example 1:

Input: nums = [1,1,0,1,1,1]

Output: 3

Example 2:

Input: nums = [1,0,1,1,0,1]

Output: 2

Example 3:

Input: nums = [0,0,0]

Output: 0

+ 7 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 10⁵
  • nums[i] is either 0 or 1

nums =

[1, 1, 0, 1, 1, 1]