15% off all plans & roadmaps Use code
See plansLeetCode 1004 · Solution & editorial
Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
Max Consecutive Ones III is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
Master variable-size sliding window for condition-based problems like longest substring without repeating characters.
The reference solution runs in O(n) time and O(1) space.
Max Consecutive Ones III has been reported in interviews at Facebook, Coupang, HBO.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Max Consecutive Ones III is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.