Advertisement

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

Longest Subarray of 1's After Deleting One Element - Complete Solution Guide

Longest Subarray of 1's After Deleting One Element is LeetCode problem 1493, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a binary array nums , you should delete one element from it. Return the size of the longest non-empty subarray containing only 1 's in the resulting array . Return 0 if there is no such subarray. Example 1: Input: nums = [1,1,0,1] Output: 3 Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's. Example 2: Input: nums = [0,1,1,1,0,1,1,0,1] Output: 5 Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with valu

Detailed Explanation

The problem asks us to find the length of the longest subarray containing only 1's in a given binary array (array of 0s and 1s), after deleting *exactly one* element. The element to be deleted can be a 0 or a 1. If deleting an element does not produce any valid non-empty subarray of 1s, we return 0. The constraints specify the array length is between 1 and 10^5, and each element is either 0 or 1. The goal is to maximize the length of the subarray of 1s after the deletion.

Solution Approach

The solution uses a sliding window approach. We maintain a window with at most one zero. We iterate through the array, expanding the window to the right. If the number of zeros in the window exceeds one, we shrink the window from the left until the number of zeros is at most one again. We keep track of the maximum length of the window at each step. Finally, we return the maximum length found minus 1 since the problem states that one element must be removed.

Step-by-Step Algorithm

  1. Step 1: Initialize `left = 0`, `zeros_count = 0`, and `max_length = 0`.
  2. Step 2: Iterate through the array from `right = 0` to `n - 1`. This is the expansion part of sliding window.
  3. Step 3: If `nums[right]` is 0, increment `zeros_count`.
  4. Step 4: While `zeros_count > 1`, shrink the window from the left. If `nums[left]` is 0, decrement `zeros_count`. Increment `left`.
  5. Step 5: Update `max_length` with the maximum of `max_length` and `right - left + 1`. This is the length of the current window.
  6. Step 6: After the loop finishes, return `max_length - 1`. This is because we need to remove one element to meet the criteria.

Key Insights

  • Insight 1: The core idea is to use a sliding window approach to efficiently explore all possible subarrays after deleting one element.
  • Insight 2: Maintain a count of zeros within the current window. The window can expand until it contains more than one zero, then the left boundary of the window needs to move until it contains at most one zero.
  • Insight 3: After iterating through the array, the resulting length is the 'max_length' minus 1 because we have counted an unnecessary element.
  • Insight 4: Need to take care when all elements in the array are 1s. When all elements are 1s, then the longest subarray is `n-1`.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming, Sliding Window.

Companies

Asked at: Tinkoff, VK, Yandex.