Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Max Consecutive Ones III

Sliding Windowmedium

Implement `longestOnes(nums, k)` returning the length of the longest subarray of 1s after flipping at most `k` zeros to 1.

Sample tests

Input: longestOnes([1,1,1,0,0,0,1,1,1,1,0], 2)
Output: 6
Input: longestOnes([0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], 3)
Output: 10

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Allowing more than k zeros in the window at once.

Learning resources

  • MDN: String
Approach & explanation (try first)

A window bounded by at most k zeros gives the longest run of ones in O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.