the prefix/suffix match approach is a lot better to understand imo
@nomi982 ай бұрын
I think people can't understand this one from any of the available videos because they're glossing over some things, which either they don't understand or they find not worth mention (or simply forget). I just wrote down step by step how I would solve it with two pointers and figured out why they were doing what they were doing. I finally understand this shit 😭.
@kulyashdahiya2529Ай бұрын
it's a DP apprach.
@prasenjitsutradhar3368Күн бұрын
I want to be honest here-this is probably the first time I’ve struggled to clearly understand Striver’s explanation! 😭😭😭 Previously, I learned the stack topic from Aditya Verma as Striver’s videos weren’t available then. For all other topics, I’ve relied on Striver and have never found any approach difficult to grasp. However, this time, I barely understood the explanation for the second approach. I would sincerely request Striver to consider re-recording this portion to provide more clarity and replace the existing video.🙏🙏🙏
@b_technical40174 ай бұрын
Striver you don't know how much this is helping us!! Thank you so so so Much! No one can explain DSA better than you❤❤😭
@shreyxnsh.142 ай бұрын
wrong problem to say this, barely anyone understood what he said in the second approach
@rwordspecialist673416 күн бұрын
Fr😂😂@@shreyxnsh.14 gotta see once more will see what I'm missing
@rushidesai28362 ай бұрын
The way you explained the second approach is commendable.
@Sonu.Singh.282 ай бұрын
Simple thought process: On top a building we can store some water if the there is a building on both left and right with height greater than the current building. And the amount of water stored on top of the building would be the min height of those left and right building minus the height of current building.
@anujbalwada5211Ай бұрын
Thanks a lot bro 🤗 with this single thought , I have implemented it easily .
@kartiksaini-xn6ke4 ай бұрын
16:46, arr[i] has to be *smaller* than and NOT greater than leftMax and rightMax to store water on top of it
@subratkumarsahoo37854 ай бұрын
Totally confused in the better approach!!😢I think the bruteforce approach is better..
@farchit94673 ай бұрын
if h[l] < h[r], meaning the right can handle the water thus we don't need to worry about the right half, thus we check the left half if we have a taller building (leftMax) to store water & act accordingly. lly for the h[l] > h[r].... for h[l] == h[r] move any doesn't matter
@chad._lifeАй бұрын
better is cup cake
@sonalidutta825Ай бұрын
read my comment. you'll get the gist of it. 😇
@vamsikrishnagannamaneni91229 күн бұрын
@@farchit9467 what does llly mean dude?
@jyotikajaichand198225 күн бұрын
@@vamsikrishnagannamaneni912 similarly
@souravmohanty8343 ай бұрын
The optimal approach would definitely not strike in an interview if not revisited😂
@Aksht-h9u2 ай бұрын
your reminder to revisit
@Imtiyazbhai-ox9dg2 ай бұрын
@@Aksht-h9u ++
@shikher45592 ай бұрын
Striver heap playlist is needed please!
@hashcodez7574 ай бұрын
CORRECTION-> 22:51 we have to take the smaller one
@omkarshendge54384 ай бұрын
yup exactly we will go with the smaller one.
@riyaraj57203 ай бұрын
yess
@nitinxd982 ай бұрын
i watched this video three times again for the opetimal approach and got confused as soon as he took the 2 instead of 1 thanks for clearing
@lillyput22752 ай бұрын
Yes I watched many times and thought to put this comment and found ur comment 😅
@oyeesharmeАй бұрын
yeah bro
@KrishnaSingh-rd6pr4 ай бұрын
Dropping stack playlist in a queue Noice
@sonalidutta825Ай бұрын
if you understood the prefix/sufix approach, you'll get the 2pointer approach as well !!!! 1. why L and R meet at the highest height? -> since we are processing left/right side based on whichever is the smallest. at some index Left_max is less than Rightt_max => process left , update Left_max now as soon as Left_max becomes greater than Rightt_max => start processing right, update Rightt_max....continue 2. why 2 pointer? amount = min(Left_max,Rightt_max) - curr_height; (and amount should be greater than 0 obvoiusly!) from prefix and suffix logic: min(Left_max,Rightt_max) => if at some point Left_max is minimum then for any next index right_max can be minimum only when left_max is updated to a bigger value. note: values of Left_max & Rightt_max can't be reduced. we are taking minimum out of bigger and bigger. coming to 2ptr now for any index L from start (i.e left) I need min(Left_max, Rightt_max). suppose for that index left_max till L-1 is less than right_max till some index R. do u think we need to calculate right_max till L+1 knowing that value of right_max won't reduce and we need the min(Left_max,Rightt_max). answer is no. Reason? -> right_max is only going to increase. so the left_max is already the minimum. then why the heck you'd calculate right_max till L+1 , isn't it great? happy coding!!🙌🏼☺
@sonalidutta825Ай бұрын
class Solution { public: int trap(vector& height) { int n = height.size(); int l=0, r=n-1, l_max=0,r_max=0, ans=0; while(l
@Gurunat163 ай бұрын
For better understanding, consider L, R, (either of) leftMax or rightMax as buildings (Building of 3)..... To trap water it should satisfy any one of the combinations... leftMax > L < R L > R < rightMax Remember you always first look at the L and R. Then decide which one to choose among leftMax or rightMax. Next doubt might be, how it works for same L and R... If L == R, then we can apply it to both of the above combinations. leftMax > L == R -> Wont be valid.... Because leftMax cannot be greater than R (Thats how we iterate thru the arr). Same way for another combination as well.
@ugthesep57064 ай бұрын
Learned a new approach I didn't know the second approach previously. I solved this problem by approach 1 on my own a few weeks ago♥
@sujalthakkar211825 күн бұрын
22:51 - shouldn't we have to take the smaller one here? please correct it. and in the portion of describing the intuition of Optimal approach as well, you said that arr[i] will always have to be greater where it's the opposite. thank you concept was crystal cleared 💌💌
@MadirajuPhaniSaiSrinivas3 ай бұрын
Corrections in video : 16:46 & 22:51 - Take Smaller One*
@apmotivationakashparmar7222 ай бұрын
Thank you Striver for great explaination 😀🙏
@killerboy23874 ай бұрын
Striver we want Heap playlist its important in intership round there is question of heap
@bilalshaikh56484 ай бұрын
hey Striver hope you are doing extremely. Just had a small request to update these video links on the take U forward A2Z DSA sheet.
@AyushEditz-hs6pf2 ай бұрын
Why is this in stack and queue playlist? Shouldn't this be int the 2 pointer and Sliding window playlist??
@Enigm.1.Ай бұрын
exactly
@oyeesharmeАй бұрын
optimal solution 🤐
@AyanAbbas-ps8xv4 ай бұрын
for the very first time solved a hard problem in a single go
@chad._lifeАй бұрын
but i just want to say your explanation is best out of all ............
@kaichang8186Ай бұрын
understood, thanks for the perfect explanation
@arnavjain60383 ай бұрын
Please link problem on code 360 in description. Great Video as always!
@Ekam8733 ай бұрын
i did one thing i traverse from the left and find next greater or equal element except for zero and then we can subtract indexs -1 and that will give the unites of water logged and sum it up that's how you will be using stack to find next smaller element
@zenmonk293 ай бұрын
i did the same.but code is a bit errenous. can u share the code please
@esmamanyt70487 күн бұрын
my thought process was same but fucked up while writing code
@DhananjayKumar-rr3jc3 ай бұрын
Amazing optimal solution
@RanjanKumar-pm4bc2 ай бұрын
Aree dhano 😅
@RanjanKumar-pm4bc2 ай бұрын
Optimal samjh aa gaye be ???? Aate h room pe
@prathameshjadhav29423 ай бұрын
I strongly advise that if not understood Better Approach then Dry run of pseudo code..
@hashcodez7574 ай бұрын
bhai na toh stack use hua na hi queue 🥲😅
@samiranroyy17003 ай бұрын
sir Understood Nice Explanation🥰🥰
@mayukhbhowmik93412 күн бұрын
Bro Made stack question a two pointer question!!!
@udaypaul773 ай бұрын
great explanation
@swarupdas411410 күн бұрын
Optimal Solution 15:40
@OmPrasad-x9u4 ай бұрын
Nice Explanation
@Adarsh_agrahari3 ай бұрын
thanks u bhaiya for this quality content
@gautamsaxena46477 күн бұрын
understood bhaiya
@sauravfarkade19284 ай бұрын
Understood!! cout
@KarumuriYasaswiniАй бұрын
optimal soln 😵
@Sridhar-fd5qrАй бұрын
Dry run, you will understand the intuition
@omkarshendge54384 ай бұрын
arr[i] has to be strictly less than leftmax and rightmax this is the correction folks pls take care of.
@yashpaunikar6713 ай бұрын
Yes. He mistakenly said greater
@Sanyam7208Ай бұрын
Doubt Ye pure process me stack kaha use hua jo ye stack ka topic ha
@AkshayVaghela-o9c4 ай бұрын
22:57 l=1 and r=2 then why you choose r=2 we have to choose smaller one right ??
@hashcodez7574 ай бұрын
same doubt??
@abishailovelson16144 ай бұрын
prolly mistake
@omkarshendge54384 ай бұрын
a mistake we should take lmax = 1 here he did a typo, pls take a note of it
@swadhinsahoo28463 ай бұрын
16:46 it has to be smaller not greater
@ddevarapaga51343 ай бұрын
Understood
@aashikroy2198Ай бұрын
Solution using Stack : public static long getTrappedWater(long []height, int n) { long totalWater = 0; Stack stack = new Stack(); for (int i = 0; i < n; i++) { while (!stack.isEmpty() && height[i] > height[stack.peek()]) { int bottom = stack.pop(); // Index of the bar at the bottom if (stack.isEmpty()) { break; } int left = stack.peek(); int right = i; int width = right - left - 1; long heightOfWater = Math.min(height[left], height[right]) - height[bottom]; totalWater += width * heightOfWater; } stack.push(i); } return totalWater;
@aayush5474Ай бұрын
aadhe ghante ki video dekhli na stack use hua na queue
@sanketatmaram2 ай бұрын
understood!
@DeadPoolx1712Ай бұрын
UNDERSTOOD;
@nikhiljain-nl7rs3 ай бұрын
if condition is not needed in the last loop
@vamsikrishnagannamaneni91217 күн бұрын
Why are we incrementing the smaller one in O(1) SC approach?
@subee1284 ай бұрын
Thanks
@THOSHI-cn6hg3 ай бұрын
two pointer approach is better
@aashikroy2198Ай бұрын
I thought he would explain the approach by using stack as well.
@KartikeyTT4 ай бұрын
tysm sir
@firebout76752 ай бұрын
class Solution { public: int trap(vector& h) { vector left(h.size(), 0); vector right(h.size(), 0); int maxi = INT_MIN; for(int i=0; i maxi){ maxi = h[i]; left[i] = maxi; } else{ left[i] = maxi; } } maxi = INT_MIN; for(int i=h.size()-1; i>=0; i--){ if(h[i] > maxi){ maxi = h[i]; right[i] = maxi; } else{ right[i] = maxi; } } int ans =0; for(int i=0; i
@samiranroyy17003 ай бұрын
class Solution { public int trap(int[] height) { int n= height.length; int left[]= new int[n]; int right[] = new int[n]; left[0]=height[0]; for(int i=1;i=0;j--) { right[j]=Math.max(right[j+1],height[j]); } int ans=0; for(int y=0;y
@themusicguy5394 ай бұрын
void lefty(vector&height,vector&prefix){ prefix[0]=height[0]; for(int j=1;j=0;j--){ suffix[j]=max(height[j],suffix[j+1]); } } int trap(vector& height) { int total=0; int n=height.size(); vectorprefix(n); vectorsuffix(n); lefty(height,prefix); righty(height,suffix); for(int i=0;i
@sibiranganath3 ай бұрын
why is this problem is under stack and queue playlist
@lokesh86602 ай бұрын
How can we do it using just the suffix sum
@KeerthiReddyKolan4 ай бұрын
But neither stack nor queue is used? 🤔🤔
@Ekam8733 ай бұрын
did one thing i traverse from the left and find next greater or equal element except for zero and then we can subtract indexs -1 and that will give the unites of water logged and sum it up that's how you will be using stack to find next smaller element
@Aksht-h9u2 ай бұрын
@@Ekam873 can you explain more or share your code please
@SapanaDashoni2 ай бұрын
Definitely unable to make better approach at interview
@KartikeyTT4 ай бұрын
ye stack and queue ka question to nhi h fir is playlist me kyu h
@yashverma20713 ай бұрын
Bhai tu padh na
@vasuchourasia80212 ай бұрын
good question🥸
@n5678--2 ай бұрын
Isme stack queue ka use kahan hua??
@sibiranganath3 ай бұрын
daam man
@mohanappriyakarunakaranАй бұрын
Fine but where do we use stack ir queue here🤔
@MuraliSriramula-lm4ztАй бұрын
where does stack used here?
@AmandeepSingh-rd6ql4 ай бұрын
Why traversing the smaller one someone can tell .?
@dhruvkhanna24103 ай бұрын
if you traverse the greater one, how can you store the water? draw a curve for the array and think....you will understand!
@dhruvkhanna24103 ай бұрын
i will look like a container
@AmandeepSingh-rd6ql3 ай бұрын
@@dhruvkhanna2410 thanks bro for the help
@rishabhkansal58114 ай бұрын
Why in l and r we always choose smaller building?
@rallapatijayasailakshmi15854 ай бұрын
the basic equation for calculating the water is water = Min (leftMax,RightMax) - currentHeight , since we are choosing the min of left and right , we are only intrested in getting the smaller building always
@Bhargob_Lahon2 ай бұрын
stack toh use hi nhi ho rha , toh stack playlist me kiu h ye
@scruffymakaveli687020 күн бұрын
I have a question Striver, why is this in your stack and queue playlist? We didn't use any stack or queue to solve this problem
@chitranshverma18224 ай бұрын
for the first approach which should have been easier was not able to code it up on leetcode....its just soo hard, it throws runtime error to me ....i know that i must be putting some minute error but still i am crying in tears and frustrated as hell PS- i am sitting and doing this question since 2 hours now
@themusicguy5394 ай бұрын
void lefty(vector&height,vector&prefix){ prefix[0]=height[0]; for(int j=1;j=0;j--){ suffix[j]=max(height[j],suffix[j+1]); } } int trap(vector& height) { int total=0; int n=height.size(); vectorprefix(n); vectorsuffix(n); lefty(height,prefix); righty(height,suffix); for(int i=0;i
@madhu_mohanreddy4 ай бұрын
second approach is better
@KUMARSAURABH-s5i4 ай бұрын
for vectors use pass by reference
@abhishekc35563 ай бұрын
class Solution { public: int trap(vector &height) { int n = height.size(); if (n == 0) return 0; int left[n]; left[0] = height[0]; int i = 1; while (i < n) { left[i] = max(left[i - 1], height[i]); i++; } int rightMax = INT_MIN; int totalWater = 0; for (int i = n - 1; i >= 0; i--) { rightMax = max(rightMax, height[i]); totalWater += min(left[i], rightMax) - height[i]; } return totalWater; } };
@AkOp-bf9vm2 ай бұрын
i have a question ... we done both approach without stack then why this question is in stack playlist??
@pranaymishra54562 ай бұрын
why this video is in stack and queue playlist? There is no use of stack or queue
@Ayush372623 ай бұрын
Why this question is marked as hard on leetcode??
@subhajitdey1353 ай бұрын
because its hard man :) prolly due to the optimal approach
@gaminghouse78613 ай бұрын
explanation is not too good , begineer will never git it.