Longest Subarray of 1's After Deleting One Element
Real-world version: Longest Green Run After One RemovalThe statement here is our own: the same inputs, outputs and technique.
A turnstile writes one entry per minute into log: 1 when it answered the gate controller that minute and 0 when it did not.
Before the log is filed, exactly one entry must be struck out -- the operator is required to remove one minute, not allowed to skip the removal. The auditor then reads the resulting array and reports the length of its longest run of consecutive 1s.
Return the largest length the auditor can be made to report. If no 1 can survive the removal, return 0.
Examples
Example 1
- Input
- log = [1, 1, 0, 1, 1, 1]
- Output
- 5
Striking out the entry at index 2 leaves five 1s in a row.
Example 2
- Input
- log = [1, 1, 1, 1]
- Output
- 3
Every minute answered, and one entry still has to go, so three remain in a row.
Example 3
- Input
- log = [1, 1, 0, 0, 1, 1, 1]
- Output
- 3
Striking out index 2 leaves [1, 1, 0, 1, 1, 1], whose longest run of 1s is three.
Example 4
- Input
- log = [0, 0, 0]
- Output
- 0
The log holds no 1 at all, so no run of 1s can survive.
Constraints
- 1 <= log.length <= 10^5
- log[i] is either 0 or 1.
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_subarray(log: list[int]) -> int:public int longestSubarray(int[] log)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 4 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
- Maximum Length of Repeated SubarrayReal-world version: Longest Shared Run of Road SegmentsMedium
- Arithmetic SlicesReal-world version: Steady-Gradient StretchesMedium
- Maximum Sum of Two Non-Overlapping SubarraysReal-world version: Two Separate Stretches Of The Output LogMedium
- Maximum Sum of 3 Non-Overlapping SubarraysReal-world version: Three Blocks on the Support RotaHard