Best explanation in every video... Thanks a lot bhaiya🙏🏻 Also in case interviewer bole ki -ve values bhi ho sakti hai array mae, toh prefix sum (hashing) use karenge na??
thanks for the explanation. i was able to solve it on my own. binary search solution if anyone is looking: basically you can try window of different size using binary search: function checkWindow(array, windowSize, target) { let sum = 0; for (let i = 0; i < windowSize; i++) sum += array[i] if (windowSize > array.length) return sum; let max = sum let [left, right] = [0, windowSize - 1]; while (right < array.length) { sum -= array[left]; left++; right++; if (right < array.length) sum += array[right]; else break; max = Math.max(max, sum); } return max >= target } /** * @param {number} target * @param {number[]} nums * @return {number} */ /** * @param {number} target * @param {number[]} nums * @return {number} */ var minSubArrayLen = function (target, nums) { let [low, high] = [1, nums.length + 1]; let ansPossible = false; while (low < high) { const mid = Math.floor((low + high) / 2); if (checkWindow(nums, mid, target)) { ansPossible = true; high = mid } else { low = mid + 1; } } return ansPossible ? high : 0; };
class Solution { public: int minSubArrayLen(int target, vector& nums) { int n=nums.size(); int i=0; int j=0; long int tempSum=0; int ans=INT_MAX; while(j
Nice explanation, brother. But i have a question. Few days ago, I started your DP series. Now, I am starting this sliding window series. can we solve this using DP as the question is asked for minimum size?? How can we understand that it need to solve with sliding window or DP?? take and skip then return min size of length? I have a little bit confusion to figure out how to solve this with dp or sliding window.
Correct me plzz if I'm wrong, my question is, let's say if we have arr=(1, 1, 1, 4) and target = 4 then in this our T.C would be n^2 bcoz index j will iterate till end to become >=target i.e 4 now we will move index i from start to end to find the shortest sub array which is greater or equal to target. So in short my question is the worst time complexity for the first code is n^2
Probably a bit late,and you most probably know the answer by now, but still saying just in case No, it won't be n^2, it would be iterating the whole array almost twice in those such cases,not n*n times. O(2N) would be more like it, and as we ignore the constants as per conventions, it's considered as O(N) only.
Bhaiya, ajkal kahan google amazon e sab puchti hai 🤧🤧. Ajkal to 4 - 5 LPA offer karne wale company bhi leetcode hard puchti hai, aur ajkal to MAANG company apne interview main bhi CP ke jaise questions puchte hai, nehi to leetcode hard, jo CP ke liye hi hote hai 🥹
Great Explanation
Thanks a lot ❤️❤️❤️
One of the finest videos on this problem. This channel is a GEM
solved without completing the whole video. thanks bhaiya, lot of improvement bcz of you!
Whenever you see a problem that involves finding contiguous sub-array (size or the sub-array itself) always consider using sliding window.
Great work, you have been a great help in maintaining streak. Thanks dude, kudos.
Thanks a lot ❤️❤️
Best explanation in every video...
Thanks a lot bhaiya🙏🏻
Also in case interviewer bole ki -ve values bhi ho sakti hai array mae, toh prefix sum (hashing) use karenge na??
Thanks a lot.❤️❤️
Yes
Leetcode - 862 is same with -ve numbers
You are the best man. This channel is the ultimate
best explanation soo far
thanks
90%+ comments from today only, goes on to show that u have grown alot 😅😂❤
Totally agree. This channel has come a long way
It means a lot to me 🙏❤️
Binge watching this playlist 🙌
thanks for the explanation. i was able to solve it on my own.
binary search solution if anyone is looking:
basically you can try window of different size using binary search:
function checkWindow(array, windowSize, target) {
let sum = 0;
for (let i = 0; i < windowSize; i++) sum += array[i]
if (windowSize > array.length) return sum;
let max = sum
let [left, right] = [0, windowSize - 1];
while (right < array.length) {
sum -= array[left];
left++;
right++;
if (right < array.length) sum += array[right];
else break;
max = Math.max(max, sum);
}
return max >= target
}
/**
* @param {number} target
* @param {number[]} nums
* @return {number}
*/
/**
* @param {number} target
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen = function (target, nums) {
let [low, high] = [1, nums.length + 1];
let ansPossible = false;
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (checkWindow(nums, mid, target)) {
ansPossible = true;
high = mid
} else {
low = mid + 1;
}
}
return ansPossible ? high : 0;
};
Thank you so much ❤️❤️❤️
Awesome and clean as always ❤
Awesome and clean ❤❤❤
Awesome explanation
Great Video👌🏻
Thank you Anup ❤️😊
Nice explanation
Best explanation ❤
Nicely explained!
Glad it was helpful! ❤️❤️
sir aap pls har playlist ki concept wali videos bhi dala karo🙏
Bhaiya...
Can elaborate why LC follow up say ( try coding another solution of which the time complexity is O(n log(n)))
Hmm sure. Let me soon plan a video on that ❤️❤️
Imagine this man explaining Trading 🤑💸
U deserve more subscribers brother
Means a lot ❤️❤️🙏🙏
class Solution {
public:
int minSubArrayLen(int target, vector& nums)
{
int n=nums.size();
int i=0;
int j=0;
long int tempSum=0;
int ans=INT_MAX;
while(j
Nice explanation, brother.
But i have a question. Few days ago, I started your DP series. Now, I am starting this sliding window series.
can we solve this using DP as the question is asked for minimum size?? How can we understand that it need to solve with sliding window or DP??
take and skip then return min size of length? I have a little bit confusion to figure out how to solve this with dp or sliding window.
Whenever you see a problem that involves finding contiguous sub-array (size or the sub-array itself) always go for sliding window.
Whenever you see a problem that involves finding contiguous sub-array (size or the sub-array itself) always consider using sliding window.
Correct me plzz if I'm wrong, my question is, let's say if we have arr=(1, 1, 1, 4) and target = 4 then in this our T.C would be n^2 bcoz index j will iterate till end to become >=target i.e 4 now we will move index i from start to end to find the shortest sub array which is greater or equal to target. So in short my question is the worst time complexity for the first code is n^2
Probably a bit late,and you most probably know the answer by now, but still saying just in case
No, it won't be n^2, it would be iterating the whole array almost twice in those such cases,not n*n times.
O(2N) would be more like it, and as we ignore the constants as per conventions, it's considered as O(N) only.
class Solution {
public:
int minSubArrayLen(int t, vector& v) {
int ans = 10000000 ,i = 0, j = 0, sum = 0, n = v.size();
while(i < n){
sum += v[i];
while(sum >= t && j < n){
ans = min(ans,i - j);
sum -= v[j];
j++;
}
i++;
}
return (ans == 10000000) ? 0 : ans+1;
}
};
Bihar me kahan se ho bhai?
My Nani Ghar is at Patna
Thanks men
Bhaiya, ajkal kahan google amazon e sab puchti hai 🤧🤧. Ajkal to 4 - 5 LPA offer karne wale company bhi leetcode hard puchti hai, aur ajkal to MAANG company apne interview main bhi CP ke jaise questions puchte hai, nehi to leetcode hard, jo CP ke liye hi hote hai 🥹
It is kind of 2 pointer approch and less about sliding window
n logn ka solution to reh hi gaya bhai