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

Why does a broken heart hurt?

TLDR 

  1. Welcome!

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

  3. Self-Gaslight of the week

  4. Hacks of the week

Hello and welcome back brave human being!

I have a question for you, whenever I open your email and I see:

We received a large number of applications, and after carefully reviewing all of them, unfortunately….

my stomach drops, my head hurts and my heart hurts.

Does the same happen to you?

I always find it odd that an email has such an impact on me.

But this week I learned, that social rejection can actually cause physical pain.

So whenever you get a rejection, breathe, let yourself be sad, and take a Tylenol.

LeetCode problem

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 

Before we dive in,

What would be our naive solution?

If your answer was finding all of the subarrays, you are correct! But you like expensive things. And I’m cheap, so let’s find an optimized solution.

The clue to identifying that it’s a sliding window problem is that you need to flip an element of the subarray.

Which means that this is a dynamic window.

Now that we got some clues…

Let’s dive in 🤿 :

We can start by defining the variables that we are going to use.

  • longestSubarray: This variable will store the length of the longest subarray.

  • countOfZeros: This variable will store the number of 0’s we’ve flipped so far.

  • Left: This pointer will indicate the start of our window.

  • Right: This pointer will indicate the end of our window.

The sliding window technique involves iterating through our string using the L and Right pointers. Right will sliiide through the string (iterate with a for loop).

At each step of the iteration, we will check if the character at R is 0. If it is, we increase the countOfZeros counter.

We will also check if the current window using:

R - L + 1 == k

We update the variable longestSubarray in case it has a larger value.

If we encounter a 1 and our countOfZeros is more than 1, it’s time for us to slide our window.

We do so by incrementing L and adjusting countOfZeros accordingly if the value at L was 1 , we’ve gained back our flip.

We iterate the array and return the variable longestSubarray .

Let’s set an example:

Final Answer:

class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int longestSubarray = 0; 
        int countOfZeros = 0; 
        int left = 0; 
        
        for (int right = 0; right < nums.size(); right++) {
            if (nums[right] == 0) {
                countOfZeros++;
            }
            
            while (countOfZeros > 1) {
                if (nums[left] == 0) {
                    countOfZeros--;
                }
                left++;
            }
            
            longestSubarray = max(longestSubarray, right - left);
        }
        
        return longestSubarray;
    }
};

What would be the O notation

Login or Subscribe to participate in polls.

Self-gaslight of the week

I truly believe you can gaslight yourself to success.

  1. I’ve saved and invested 70k (401+ Roth IRA) within two working years in startups and it’s because I’ve gaslighted myself into understanding money by reading this book .

  2. I always gaslight myself into working more by hearing this playlist

Hacks of the week

  1. When I was 21 I went across South America selling Mexican beauty products and met with the most important retailers, I owe it all to knowing how to scrape emails, using the snov.io Chrome extension.

  2. Want to get featured in a publication? Add all of the editors on Twitter!!!

Cheers to you hacking your week,

Denisse

How would you rate the newsletter so far?

Login or Subscribe to participate in polls.

Reply

or to participate.