Max Consecutive Ones III
Real-world version: Longest Verified Backup StreakThe statement here is our own: the same inputs, outputs and technique.
A storage team keeps a nightly integrity log. checks holds one entry per night in chronological order: 1 means that night's snapshot was checked, 0 means the check was skipped.
There is spare capacity to go back and check at most k of the skipped nights, and the team may choose whichever skipped nights it likes.
Report the greatest number of neighbouring nights that can all end up checked once at most k retro-checks have been spent. Spare capacity may go unused, and the nights picked for a retro-check need not sit next to each other.
Examples
Example 1
- Input
- checks = [1, 1, 0, 1, 1, 0, 1], k = 1
- Output
- 5
Spending the single retro-check on night 2 leaves nights 0 through 4 all checked, five nights wide. Any six neighbouring nights here hold two gaps, which is one retro-check too many.
Example 2
- Input
- checks = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0], k = 2
- Output
- 7
Retro-checking nights 4 and 5 fills the only gaps inside nights 2 through 8, so seven neighbouring nights end up checked.
Example 3
- Input
- checks = [0, 0, 0], k = 0
- Output
- 0
Nothing was checked and there is no spare capacity, so no night can be part of a checked block.
Constraints
- 1 <= checks.length <= 10^5
- checks[i] is either 0 or 1.
- 0 <= k <= checks.length
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_verified_streak(checks: list[int], k: int) -> int:public int longestVerifiedStreak(int[] checks, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Subarray Product Less Than KReal-world version: Safe Stretches of the Filter CascadeMedium
- Minimum Size Subarray SumReal-world version: Fewest Shifts to Clear the QuotaMedium
- Frequency of the Most Frequent ElementReal-world version: Plates at One ThicknessMedium
- Minimum Operations to Reduce X to ZeroReal-world version: Loading Crates From Both EndsMedium