← Home

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

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
sliding windowC++Markdown
149

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1493. Longest Subarray of 1's After Deleting One Element, the solution in this repository is mainly a sliding window solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: sliding window.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are longestSubarray.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 92 ms, faster than 88.93% of C++ online submissions for Longest Subarray of 1's After Deleting One Element.
02//Memory Usage: 36.7 MB, less than 25.00% of C++ online submissions for Longest Subarray of 1's After Deleting One Element.
03class Solution {
04public:
05    int longestSubarray(vector<int>& nums) {
06        int slow = 0, fast = 0;
07        int ans = 0;
08        int n = nums.size();
09        int last0 = -1;
10        
11        while(fast < n){
12            while(slow < n && nums[slow] == 0) ++slow;
13            if(slow == n) break;
14            fast = max(slow, fast);
15            int os = slow, of = fast;
16            
17            if(nums[fast] == 1){
18                //last0 >= slow: whether cross a 0
19                ans = max(ans, fast - slow + 1 - (last0 >= slow));
20                ++fast;
21            }else if(last0 < slow){
22                //cout << "cross0" << endl;
23                last0 = fast;
24                ++fast;
25            }else{
26                //cout << "new window" << endl;
27                slow = last0+1;
28                //nums[fast] is 0
29                fast = fast-1;
30            }
31            //cout << "[" << os << ", " << of << "]: " << ans << endl;
32        }
33        
34        //if all 1, we must remove a 1
35        return (ans == n) ? ans-1 : ans;
36    }
37};
38
39//sliding window with at most one 0 inside
40//https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/discuss/708112/JavaC%2B%2BPython-Sliding-Window-at-most-one-0
41//Runtime: 132 ms, faster than 52.03% of C++ online submissions for Longest Subarray of 1's After Deleting One Element.
42//Memory Usage: 36.7 MB, less than 25.00% of C++ online submissions for Longest Subarray of 1's After Deleting One Element.
43class Solution {
44public:
45    int longestSubarray(vector<int>& nums) {
46        int k = 1, ans = 0;
47        for(int slow = 0, fast = 0; fast < nums.size(); ++fast){
48            /*
49            by including nums[fast] to our window,
50            we use one quota of 0,
51            so now we allow k-1 more 0s to be added
52            */
53            if(nums[fast] == 0) --k;
54            
55            /*
56            k must be >=0,
57            so we shrink our window to 
58            make the condition hold
59            */
60            while(k < 0){
61                if(nums[slow] == 0){
62                    ++k;
63                }
64                ++slow;
65            }
66            
67            /*
68            length-1 of longest subarray
69            */
70            ans = max(ans, fast - slow + 1 - 1);
71        }
72        
73        return ans;
74    }
75};
76
77//sliding window that size doesn't shrink
78//1004. Max Consecutive Ones III
79//https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/discuss/708112/JavaC%2B%2BPython-Sliding-Window-at-most-one-0
80//Runtime: 152 ms, faster than 38.66% of C++ online submissions for Longest Subarray of 1's After Deleting One Element.
81//Memory Usage: 36.7 MB, less than 25.00% of C++ online submissions for Longest Subarray of 1's After Deleting One Element.
82class Solution {
83public:
84    int longestSubarray(vector<int>& nums) {
85        int slow, fast;
86        int k = 1;
87        
88        for(slow = 0, fast = 0; fast < nums.size(); ++fast){
89            if(nums[fast] == 0) --k;
90            if(k < 0 && nums[slow++] == 0) ++k;
91        }
92        
93        //window size - deleted element
94        return fast-slow-1;
95    }
96};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.