the prefix/suffix match approach is a lot better to understand imo
@nomi983 ай бұрын
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 😭.
@kulyashdahiya25293 ай бұрын
it's a DP apprach.
@sudipsarkar1512Ай бұрын
@@kulyashdahiya2529 how so, bro ?
@b_technical40175 ай бұрын
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.144 ай бұрын
wrong problem to say this, barely anyone understood what he said in the second approach
@rwordspecialist6734Ай бұрын
Fr😂😂@@shreyxnsh.14 gotta see once more will see what I'm missing
@ajaymishra151112 күн бұрын
@@shreyxnsh.14 well this problem is actually pretty hard to explain you will need to imagine the whats actually happening
@Sonu.Singh.283 ай бұрын
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.
@anujbalwada52112 ай бұрын
Thanks a lot bro 🤗 with this single thought , I have implemented it easily .
@hashcodez7575 ай бұрын
CORRECTION-> 22:51 we have to take the smaller one
@omkarshendge54385 ай бұрын
yup exactly we will go with the smaller one.
@riyaraj57205 ай бұрын
yess
@nitinxd984 ай бұрын
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
@lillyput22754 ай бұрын
Yes I watched many times and thought to put this comment and found ur comment 😅
@oyeshxrme3 ай бұрын
yeah bro
@kartiksaini-xn6ke5 ай бұрын
16:46, arr[i] has to be *smaller* than and NOT greater than leftMax and rightMax to store water on top of it
@subratkumarsahoo37855 ай бұрын
Totally confused in the better approach!!😢I think the bruteforce approach is better..
@farchit94674 ай бұрын
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._life3 ай бұрын
better is cup cake
@sonalidutta8252 ай бұрын
read my comment. you'll get the gist of it. 😇
@vamsikrishnagannamaneni9122 ай бұрын
@@farchit9467 what does llly mean dude?
@jyotikajaichand19822 ай бұрын
@@vamsikrishnagannamaneni912 similarly
@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.🙏🙏🙏
@parasarora586916 күн бұрын
Sometimes we don't understand the why behind someone else's thinking. Give it some time and try yourself to find why it works. I also didn't get this one in the first try. But the more I thought about it, the clearer it became. Also, the first approach of using prefixMax and suffixMax is great and can be used in other problems. All the best and keep improving.
@MadirajuPhaniSaiSrinivas5 ай бұрын
Corrections in video : 16:46 & 22:51 - Take Smaller One*
@KrishnaSingh-rd6pr5 ай бұрын
Dropping stack playlist in a queue Noice
@souravmohanty8344 ай бұрын
The optimal approach would definitely not strike in an interview if not revisited😂
@Aksht-h9u4 ай бұрын
your reminder to revisit
@Imtiyazbhai-ox9dg4 ай бұрын
@@Aksht-h9u ++
@arcane__17299 күн бұрын
+++
@HarshKumar-mx9nj10 күн бұрын
For those who cannot understand approach-2: According to the code of second approach given by striver there is mistake in dry run at 22:52 where he chose building of height 2 instead of 1 I think this was the mistake which made it difficult to understand approach 2
@Diya-zb5xuАй бұрын
it's stack and queue playlist and you are giving optimal solution of two pointer. way to go . atleast could have given the optimal stack use approach.
@sonalidutta8252 ай бұрын
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!!🙌🏼☺
@sonalidutta8252 ай бұрын
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
@rushidesai28363 ай бұрын
The way you explained the second approach is commendable.
@Gurunat164 ай бұрын
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.
@ugthesep57065 ай бұрын
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♥
@shikher45594 ай бұрын
Striver heap playlist is needed please!
@killerboy23875 ай бұрын
Striver we want Heap playlist its important in intership round there is question of heap
@bilalshaikh56485 ай бұрын
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.
@sujalthakkar21182 ай бұрын
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 💌💌
@apmotivationakashparmar7223 ай бұрын
Thank you Striver for great explaination 😀🙏
@premkulkarni757815 күн бұрын
class Solution { public: int trap(vector& nums) { int n = nums.size(); // 1st step vector prefixmax(n); prefixmax[0] = nums[0]; for (int i=1 ; i=0 ; i--){ suffixmax[i] = max(suffixmax[i+1] , nums[i]); } // 3rd final step int answer = 0; for (int i=0 ; i
@hashcodez7575 ай бұрын
bhai na toh stack use hua na hi queue 🥲😅
@oyeshxrme3 ай бұрын
optimal solution 🤐
@AyushEditz-hs6pf3 ай бұрын
Why is this in stack and queue playlist? Shouldn't this be int the 2 pointer and Sliding window playlist??
@Enigm.1.2 ай бұрын
exactly
@swarupdas4114Ай бұрын
Optimal Solution 15:40
@prathameshjadhav29424 ай бұрын
I strongly advise that if not understood Better Approach then Dry run of pseudo code..
@Sanyam72082 ай бұрын
Doubt Ye pure process me stack kaha use hua jo ye stack ka topic ha
@AkshayVaghela-o9c5 ай бұрын
22:57 l=1 and r=2 then why you choose r=2 we have to choose smaller one right ??
@hashcodez7575 ай бұрын
same doubt??
@abishailovelson16145 ай бұрын
prolly mistake
@omkarshendge54385 ай бұрын
a mistake we should take lmax = 1 here he did a typo, pls take a note of it
@KeerthiReddyKolan5 ай бұрын
But neither stack nor queue is used? 🤔🤔
@Ekam8734 ай бұрын
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-h9u4 ай бұрын
@@Ekam873 can you explain more or share your code please
@AyanAbbas-ps8xv5 ай бұрын
for the very first time solved a hard problem in a single go
@Ekam8734 ай бұрын
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
@zenmonk294 ай бұрын
i did the same.but code is a bit errenous. can u share the code please
@esmamanyt7048Ай бұрын
my thought process was same but fucked up while writing code
@Ekam87314 сағат бұрын
@@zenmonk29 hi i believe my approach will not work bcz we nedd greatest on right not greater
@Ekam87314 сағат бұрын
@@esmamanyt7048 hi i believe my approach will not work bcz we nedd greatest on right not greater
@samiranroyy17004 ай бұрын
sir Understood Nice Explanation🥰🥰
@chad._life3 ай бұрын
but i just want to say your explanation is best out of all ............
@kaichang81863 ай бұрын
understood, thanks for the perfect explanation
@arnavjain60384 ай бұрын
Please link problem on code 360 in description. Great Video as always!
@DhananjayKumar-rr3jc4 ай бұрын
Amazing optimal solution
@RanjanKumar-pm4bc4 ай бұрын
Aree dhano 😅
@RanjanKumar-pm4bc4 ай бұрын
Optimal samjh aa gaye be ???? Aate h room pe
@Adarsh_agrahari5 ай бұрын
thanks u bhaiya for this quality content
@scruffymakaveli68702 ай бұрын
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
@sibiranganath4 ай бұрын
why is this problem is under stack and queue playlist
@KarumuriYasaswini3 ай бұрын
optimal soln 😵
@santoshkumarag72710 күн бұрын
One quick question here, 1st approach pre computation(prefix/Suffix). 2nd approach two pointer. Why this has been tagged in stack or queue playlist. Any thoughts?
@omkarshendge54385 ай бұрын
arr[i] has to be strictly less than leftmax and rightmax this is the correction folks pls take care of.
@yashpaunikar6715 ай бұрын
Yes. He mistakenly said greater
@aashikroy21983 ай бұрын
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;
@Sridhar-fd5qr3 ай бұрын
Dry run, you will understand the intuition
@sauravfarkade19285 ай бұрын
Understood!! cout
@godthunderbolt219015 күн бұрын
I tried to solve the Trapping Water -II with this approach but it failed on 19 test case Tell me where I am doing wrong class Solution { public: int trapRainWater(vector& heightMap) { // Here I have to see max for each side int m=heightMap.size(); // no of rows int n=heightMap[0].size(); // no of columns vectorleftrowwisemax(m,vector(n,0)); vectorrightrowwisemax(m,vector(n,0)); vectoruppercolwisemax(m,vector(n,0)); vectordowncolwisemax(m,vector(n,0)); for(int i=0;i
@vamsikrishnagannamaneni912Ай бұрын
Why are we incrementing the smaller one in O(1) SC approach?
@mayukhbhowmik934Ай бұрын
Bro Made stack question a two pointer question!!!
@udaypaul774 ай бұрын
great explanation
@MuraliSriramula-lm4zt2 ай бұрын
where does stack used here?
@mohanappriyakarunakaran2 ай бұрын
Fine but where do we use stack ir queue here🤔
@swadhinsahoo28465 ай бұрын
16:46 it has to be smaller not greater
@gautamsaxena4647Ай бұрын
understood bhaiya
@OmPrasad-x9u5 ай бұрын
Nice Explanation
@KartikeyTT5 ай бұрын
ye stack and queue ka question to nhi h fir is playlist me kyu h
@yashverma20714 ай бұрын
Bhai tu padh na
@vasuchourasia80214 ай бұрын
good question🥸
@firebout76753 ай бұрын
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
@pranaymishra54564 ай бұрын
why this video is in stack and queue playlist? There is no use of stack or queue
@samiranroyy17004 ай бұрын
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
@n5678--3 ай бұрын
Isme stack queue ka use kahan hua??
@themusicguy5395 ай бұрын
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
@AmandeepSingh-rd6ql5 ай бұрын
Why traversing the smaller one someone can tell .?
@dhruvkhanna24105 ай бұрын
if you traverse the greater one, how can you store the water? draw a curve for the array and think....you will understand!
@dhruvkhanna24105 ай бұрын
i will look like a container
@AmandeepSingh-rd6ql5 ай бұрын
@@dhruvkhanna2410 thanks bro for the help
@aayush54742 ай бұрын
aadhe ghante ki video dekhli na stack use hua na queue
@nikhiljain-nl7rs4 ай бұрын
if condition is not needed in the last loop
@aashikroy21983 ай бұрын
I thought he would explain the approach by using stack as well.
@ddevarapaga51344 ай бұрын
Understood
@lokesh86603 ай бұрын
How can we do it using just the suffix sum
@shoaibaltaf112814 күн бұрын
By maintaining a variable that stores the leftmax and is updated while traversing from left to right
@rishabhkansal58115 ай бұрын
Why in l and r we always choose smaller building?
@rallapatijayasailakshmi15855 ай бұрын
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
@devireddysaikumarreddy47729 күн бұрын
Plse pop() this video from the stack playlist 😂
@DeadPoolx17123 ай бұрын
UNDERSTOOD;
@sanketatmaram3 ай бұрын
understood!
@chitranshverma18225 ай бұрын
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
@themusicguy5395 ай бұрын
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_mohanreddy5 ай бұрын
second approach is better
@KUMARSAURABH-s5i5 ай бұрын
for vectors use pass by reference
@abhishekc35564 ай бұрын
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; } };
@Bhargob_Lahon4 ай бұрын
stack toh use hi nhi ho rha , toh stack playlist me kiu h ye
@subee1285 ай бұрын
Thanks
@THOSHI-cn6hg5 ай бұрын
two pointer approach is better
@SapanaDashoni3 ай бұрын
Definitely unable to make better approach at interview
@AkOp-bf9vm3 ай бұрын
i have a question ... we done both approach without stack then why this question is in stack playlist??
@JITESHSINGH-l7d18 күн бұрын
Is it not a 2 pointer rather than stack 😂
@KartikeyTT5 ай бұрын
tysm sir
@sibiranganath4 ай бұрын
daam man
@Ayush372625 ай бұрын
Why this question is marked as hard on leetcode??
@subhajitdey1355 ай бұрын
because its hard man :) prolly due to the optimal approach
@gaminghouse78615 ай бұрын
explanation is not too good , begineer will never git it.