All problems
0377MediumArrayDynamic ProgrammingSliding Window

Longest Green Run After One Removal

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1493Longest Subarray of 1's After Deleting One Element

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def longest_subarray(log: list[int]) -> int:
Java
public int longestSubarray(int[] log)
September 7
Apply