The longest run of 1s if you may flip at most k zeros. From trying every range (O(n²)), to prefix counts of zeros with binary search (O(n log n)), to a window that counts zeros and shrinks when it holds too many (O(n)). Then k = 0, returning which zeros to flip, a stream, and the longest healthy stretch of a service allowing k failed checks.
about 10 min 4 Python examples 2 quick checks
Problem.nums holds only 0s and 1s. You may flip at most k zeros to 1. Return the length of the longest
run of consecutive 1s you can make.
text
nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], k = 2 → 6 (flip the 0s at 4 and 5: indices 4..9)
nums = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], k = 3 → 10
ML context. Read 1 as "health check passed" and 0 as "failed": the longest stretch of uptime allowing k
failures is how you measure stability of a service, a training job (steps without a NaN, tolerating k retries) or
a data feed (hours with data, tolerating k gaps). It's the binary case of lesson 4.4.
1. Step 1: Clarify
That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson
Unlock "Max Consecutive Ones III"
The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.
Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied