- Denisse's Newsletter
- Posts
- 209. Minimum Size Subarray Sum
209. Minimum Size Subarray Sum
What can deadlines do for you?

TLDR
Welcome!
209. Minimum Size Subarray Sum
Self-Gaslight of the week (Includes a laugh 🎁 )
Hacks of the week
Hello and welcome back brave human being!
To reach your goals faster you need an external deadline.
Want to get good at LeetCode? Land an interview that will speed things up like no other.
Want to get expert-level LeetCode? Be on a visa and have a deadline with USIC.
No joke, any goal I’ve reached light speed when I have a deadline.
Otherwise, the trouble is, we think we have time.
LeetCode problem
Day 29: 209. Minimum Size Subarray Sum
Given an array of positive integers nums and a positive integer target, return the minimal length of a
subarray whose sum is greater than or equal to target
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 see an optimized solution.
The clue to identifying that it’s a sliding window problem is that you don’t have to compute the sum of each subarray; you can compute the next subarray by modifying any limit (Left or Right pointer) of the previous subarray.
This is a variable-sized window problem. We are going to adjust the Left or Right pointers in our array depending on our current window’s sum.
Now that we got some clues…
Let’s dive in 🤿 :
We can start by defining the variables that we are going to use.
currentSum: This variable will store the current Sum of elements.
minLength: This variable will store our answer, the minimum length our subarray sums to our target
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 left
and right
pointers. right
will sliiide through the array (iterate with a for loop).
At each step of the iteration, we will sum our current value to currentSum
.
If currentSum
reaches or exceeds the target, we need to determine if there's a smaller subarray that also meets the target.
To do this, we need to remove the elements that are no longer needed. (Like your ex 💅 ).
currentSum -= nums[left];
left++;
Not before calculating the current size of the window by doing:
right - left + 1 == k
And updating the variable minLength
in case currentSum
has a smaller value.
minLength = min(minLength, right - left + 1)
We reduce the size of the window until we have space to add another element.
Once we are done iterating the array, we must check if we ever found a subarray that summed up to our target, otherwise, we return 0.
Let’s set an example:

Final Answer:
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int currentSum = 0;
int minLength = INT_MAX;
int left = 0;
for (int right = 0; right < nums.size(); right++) {
currentSum += nums[right];
while (currentSum >= target) {
minLength = min(minLength, right - left + 1);
currentSum -= nums[left];
left++;
}
}
return (minLength == INT_MAX) ? 0 : minLength;
}
};
What would be the O notation |
Keep up with AI
How do you keep up with the insane pace of AI? Join The Rundown — the world’s largest AI newsletter that keeps you up-to-date with everything happening in AI with just a 5-minute read per day.
Self-gaslight of the week
I truly believe you can gaslight yourself to success.
FOLLOW WHO YOU FOLLOW, this is the easiest hack to gaslight yourself to know more to find your idol’s idol.
Gaslight yourself into thinking the interviewer at some point also sucked at LeetCode
Hacks of the week
Spend time optimizing your LinkedIn, doing so will do wonders for your career. I’ve spent time doing so and my inbox looks like this:
Consistency can’t be bought but the motivation to be consistent can!!!…book the course, the coaching, the workout class, the event!
Cheers to you hacking your week,
Denisse
Reply