using two pointer left and right . let n = nums.size(); let ans = Number.MAX_SAFE_INTEGER; let l = 0,r = 0,sum = 0; while(r=target){ if(sum>=target){ ans = min(ans,r-l+1); sum -= nums[l++]; } } r++; } if(ans== Number.MAX_SAFE_INTEGER) return 0; return ans;
Thanks James, really good explanation and not rushed through. Would love for this to be a whole series for the 150 problems maybe? Thanks again! Good luck!
Yeah bro personally I am interested of the solution of 0(n log(n)) and this kind of challenges,so please continue posting that kind of videos , but essentially it was a wonderful explanation congratulations
For me, any time I can get the index's mixed up it becomes especially hard. Things like sliding window or zig zag 2d arrays throw me off. Thanks for the content!
Also, in the leetcode description for the follow up, O(n) is better in time complexity than O(n log(n)), right? Maybe they meant to put O(log(n)) instead?
@JamesQQuick please check this solution. No nested for loops , no nested if condition . Please pin , if correct. I am new to DSA. let nums = [2, 3, 1, 2, 4, 3]; let target = 7; var minSubArrayLen = function (target, nums) { let finalLength = nums.length; let initialIndex = 0; let previousTotal = nums[0]; let i = 0; let sumQueue = [nums[0]]; while (i < nums.length ) { if (previousTotal >= target) { finalLength = Math.min(finalLength, i + 1 - initialIndex); let val = sumQueue.shift(); previousTotal -= val; initialIndex = initialIndex + 1; } else { i = i + 1; sumQueue.push(nums[i]); previousTotal += nums[i]; } } return finalLength; }; console.log(minSubArrayLen(target, nums));
First of all thank you for calling sliding window problems hard!! So nice to have someone validate that for a change, instead of calling them all easy
Oh I think it’s super tricky especially early on. You’re not alone at all!
using two pointer left and right .
let n = nums.size();
let ans = Number.MAX_SAFE_INTEGER;
let l = 0,r = 0,sum = 0;
while(r=target){
if(sum>=target){
ans = min(ans,r-l+1);
sum -= nums[l++];
}
}
r++;
}
if(ans== Number.MAX_SAFE_INTEGER) return 0;
return ans;
Thanks James, really good explanation and not rushed through. Would love for this to be a whole series for the 150 problems maybe? Thanks again! Good luck!
Loved getting to see this live. Thank you, James, and keep them coming.
first i think sliding window algo is hard, then i realised it's very simple. Thank for the tutorial.
Yeah bro personally I am interested of the solution of 0(n log(n)) and this kind of challenges,so please continue posting that kind of videos , but essentially it was a wonderful explanation congratulations
For me, any time I can get the index's mixed up it becomes especially hard. Things like sliding window or zig zag 2d arrays throw me off. Thanks for the content!
Happy to help!
Also, in the leetcode description for the follow up, O(n) is better in time complexity than O(n log(n)), right? Maybe they meant to put O(log(n)) instead?
no O(n) scales multiplicatively and O(n log(n)) scales logarithmically(think backwards shrinking exponents).
@@LeetStack You're 100% correct. I always get confused on logarithmic vs quasilinear complexities, in terms of which is the faster one 😅
Thanks James it really good.....
Great explanation! Thank you for the video!
So glad you enjoyed it! Have you done sliding window problems before?
Thanks James this is fun
So glad you enjoyed it. I have fun with these
0:41 that company is going through some budget cuts and fresh dry erase markers just aren't in the picture
Nothing clear , but veeery interesting))
Anything I could help explain better?
nice
@JamesQQuick please check this solution.
No nested for loops , no nested if condition . Please pin , if correct. I am new to DSA.
let nums = [2, 3, 1, 2, 4, 3];
let target = 7;
var minSubArrayLen = function (target, nums) {
let finalLength = nums.length;
let initialIndex = 0;
let previousTotal = nums[0];
let i = 0;
let sumQueue = [nums[0]];
while (i < nums.length ) {
if (previousTotal >= target) {
finalLength = Math.min(finalLength, i + 1 - initialIndex);
let val = sumQueue.shift();
previousTotal -= val;
initialIndex = initialIndex + 1;
} else {
i = i + 1;
sumQueue.push(nums[i]);
previousTotal += nums[i];
}
}
return finalLength;
};
console.log(minSubArrayLen(target, nums));
your solution doesn't account for if the array has 0 subarrays