↓Skip to main content
  1. LeetCode/

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

·2 mins· ·
LeetCode Medium Sliding-Window Dynamic-Programming Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-08-24

  • Submissions: Accepted ×2

Approach
#

A subarray that is all 1s after deleting one element was a subarray with at most one 0, and the deleted element is that 0 (or any 1 if there is no 0).

So find the longest window with at most one 0; the answer is window length − 1:

  • Add elements on the right; shrink the left while there is more than one 0.
  • Update with right - left (the length minus 1).

All 1s still require one deletion, giving n - 1, which this handles naturally.

My Solution
#

class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        left = zeros = best = 0
        for right, x in enumerate(nums):
            zeros += x == 0
            while zeros > 1:
                zeros -= nums[left] == 0
                left += 1
            best = max(best, right - left)         # window length - 1
        return best

Both accepted versions were similar; the second had a cleaner while condition (the first also checked nums[right] == 0).

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Returning the window length instead of length minus 1: one deletion is mandatory.
  • With all 1s the answer is n - 1, not n.

Interview Explanation
#

Being all 1s after one deletion means the original subarray had at most one 0. I slide a window that allows at most one 0, shrinking the left when there are two. One element must be deleted, so the answer is the window length minus 1, which also covers the all-1s case. Time O(n).

Related

LeetCode 3638: Maximum Balanced Shipments
·2 mins
LeetCode Medium Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 3638 note: greedily cut a segment as soon as a value below its maximum appears; until then the segment is non-decreasing, so its maximum is the previous value
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 473: Matchsticks to Square
·2 mins
LeetCode Medium Array Dynamic-Programming Backtracking Bit-Manipulation
LeetCode 473 note: the total must be divisible by 4; place sticks from longest to shortest into one of four sides, skipping a side that would overflow or that equals a side already tried