DP 39. Buy and Sell Stocks With Cooldown | Recursion to Space Optimisation

  Рет қаралды 98,461

take U forward

take U forward

Күн бұрын

Пікірлер: 264
@rupamhari115
@rupamhari115 2 жыл бұрын
After watching previous buy and sell stocks problems, I solved variant IV, with cooldown and with fee by my own, without watching the videos. 1-2 months back, I had no concept of Recursion and DP, and now, I am confident to solve any kind of recursion and DP. Thank you so much bhaiyaaa. Huge respect to you. 🙏🙏🙏🙏
@kazuma0803
@kazuma0803 2 жыл бұрын
Same
@bite091_jamee7
@bite091_jamee7 2 жыл бұрын
same
@asdsd-wu4yw
@asdsd-wu4yw Жыл бұрын
those who are seeing this kind of comments dont worry u can also solve these problems ,there is only one word of change in code as compared to previous code,these are very simple
@satendra6200
@satendra6200 Жыл бұрын
same here bro
@happysatan5554
@happysatan5554 20 күн бұрын
Where are u working now bro
@nupur3177
@nupur3177 2 жыл бұрын
Want to share the significant difference between this channel and other million channels who claim that they teach DSA, other channels explain the code or sometimes when channel is good ,intuition as well, but striver neve fails to teach the concept behind every f problem, he will explain one problem, and you can do others extremely fast(relatively) and on your own as well because he did not give you a stupid reading of code, but he explained you how to generate the f code. Only Aditya Verma bhaiya's channel along with this channel has even come close to this level of DSA teaching, this has surpassed the GOD level as well. Kudos to striver !!!!
@anandkalpe2155
@anandkalpe2155 2 жыл бұрын
Solved previous two and this problem by my own just by watching your first two videos on stocks. Completed from recursion upto space optimization...Feeling good. Thanks striver for making it very simple !
@Tejas-Coder
@Tejas-Coder Ай бұрын
After watching buy and sell stocks problems, I solved this problem by my own without any hints or watching videos. Thank you so much Sir!
@yeddulaharshitha4405
@yeddulaharshitha4405 Жыл бұрын
Thats insane ! I started DP 3 days ago. On the 4th day I am on this lecture. After watching all the buy and sell stocks, I was able to get the crisp of this in just few seconds.Kudos to you!!
@jayrathod7957
@jayrathod7957 10 ай бұрын
ohh just how??? for me,it took 1 month to reach till here...
@aditya14-02
@aditya14-02 3 ай бұрын
​@@jayrathod7957++
@calvincruzada1016
@calvincruzada1016 2 жыл бұрын
watched from 1-40 of your DP videos. I feel like I can solve any DP problem now, thanks man :)
@parthsalat
@parthsalat 2 жыл бұрын
Then why are you commenting on video 39?
@calvincruzada1016
@calvincruzada1016 2 жыл бұрын
@@parthsalat rewatching? I don't have photographic memory unfortunately :(
@parthsalat
@parthsalat 2 жыл бұрын
@@calvincruzada1016 In that case, I recommend you to use notion. It's the best note taking tool out there.
@calvincruzada1016
@calvincruzada1016 2 жыл бұрын
@@parthsalat Good advice, I love notion! Been using it for years, it's my brain dump so I don't have to keep much in my head 😂
@MEGHAPRAJAPATI-r3m
@MEGHAPRAJAPATI-r3m Ай бұрын
we can space optimize because we don't have to carry entire row for i+2, only have to carry dp[i+2][1]. so we can use a variable to store it and update it with next2 = next[1]. this is the space optimized solution: int maxProfit(vector& prices) { int n = prices.size(); vector next(2, 0), curr(2, 0); int next2 = 0; for(int i = n-1; i >= 0; i--) { curr[0] = max(next2 + prices[i], next[0]); curr[1] = max(next[0] - prices[i], next[1]); next2 = next[1]; next = curr; } return next[1]; }
@piyushpandey244
@piyushpandey244 2 жыл бұрын
i also tried using one extra variable for the two ahead variable here is the code int Maxprofit(vector&prices){ int n = prices.size(); vectorahead(2,0); vectorcurr(2,0); int twoahead = 0; for(int ind =n-1;ind>=0;ind--){ for(int buy=0;buy
@sricharanreddy9989
@sricharanreddy9989 Ай бұрын
After understanding the pattern i could solve these questions listening to music in like 5mins , your explanation is just amazing bro ❤‍🔥
@viratlover6206
@viratlover6206 7 ай бұрын
I was able to solve this problem from recursion to the space optimization by own. Thank u so much sir!
@SumitKeshariMCS
@SumitKeshariMCS Жыл бұрын
Solved it using 3D DP concept taught by you. I didnt watch the video and tried to solve it by myself. class Solution { private: int solve(int index,int n,int buy,int cool,vector& prices,vector& dp) { if(index==n) return 0; if(dp[index][buy][cool]!=-1) return dp[index][buy][cool]; int profit; if(buy==0 && cool==0) { int op1 = solve(index+1,n,0,cool,prices,dp); int op2 = -prices[index]+solve(index+1,n,1,cool,prices,dp); profit = max(op1,op2); } else profit = solve(index+1,n,0,0,prices,dp); if(buy==1) { int op1 = solve(index+1,n,1,cool,prices,dp); int op2 = prices[index]+solve(index+1,n,0,1,prices,dp); profit = max(op1,op2); } return dp[index][buy][cool] = profit; } public: int maxProfit(vector& prices) { int n = prices.size(); vectordp(n+1,vector(2,vector(2,0))); for(int index=n-1;index>=0;index--) { for(int buy=0;buy
@yugal8627
@yugal8627 Жыл бұрын
Bhaiya watched all your DP videos till now and I'm able to solve the previous and this questions by myself. Thanks a lot..🙏🏻🙏🏻
@mdamirhussain2095
@mdamirhussain2095 Ай бұрын
Understood... Thank u Striver
@ntgrn-pr5yx
@ntgrn-pr5yx 29 күн бұрын
Thank you striver , Understood
@lexeve5298
@lexeve5298 Жыл бұрын
Thanks striver , I was able to come up with the logic myself
@UECAshutoshKumar
@UECAshutoshKumar 5 ай бұрын
Thank You! Understood!!!!
@princeshubham26
@princeshubham26 Жыл бұрын
solved by myself ,,,self confidence is building
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
Understood, got the logic by just reading the problem on leetcode and even did it myslef with both memoization and tabulation technique!!! Tysm Striverrrr!!!
@preetkatiyar969
@preetkatiyar969 2 жыл бұрын
Sir you are really a gem . These topic are really tough but you teach like this is just a mind game. Best teacher
@aligohar1708
@aligohar1708 2 жыл бұрын
the power of cp can be seen by his speed .... insanely impressed... I don't know, this guy does not even need interviews for companies, he should just be directly hired
@divyansh2212
@divyansh2212 2 жыл бұрын
I solved this question on my own without watching this video before. Thankyou Bhaiya
@shubhamjaiswal1325
@shubhamjaiswal1325 Жыл бұрын
Best Explanation so far. Very intuitive. Thanks a ton
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
understood,able to solve by myself.Gaining confidence to solve dp questions
@DevashishJose
@DevashishJose 10 ай бұрын
Understood Thank you so much.
@Sid-ci1cd
@Sid-ci1cd 3 ай бұрын
UNDERSTOOD!!
@satyamgupta1446
@satyamgupta1446 2 жыл бұрын
I was able to do this question on my own. Thanks striver for helping us out...😇
@Superheroic_Anime
@Superheroic_Anime 2 жыл бұрын
Understood bhaiya !!! By just watching first 6 minutes of your video.
@shubhankar_naik
@shubhankar_naik Жыл бұрын
I was able to solve without seeing your solution thanks for the great explanations
@johnsimon8158
@johnsimon8158 20 күн бұрын
thanks for this awesome content
@VinayKumar-xs6el
@VinayKumar-xs6el 2 ай бұрын
Again i solved after pausing at 2:23 & i did on my own thanks from recursion to space optimization (java people has to use clone() for assigning arrays). not only this i did next lecture problem as well even without opening that from recursion to space optimization. thanks is small thing
@ganeshkamath89
@ganeshkamath89 2 жыл бұрын
Thanks Striver. Understood.
@mohitsingh13
@mohitsingh13 2 ай бұрын
Understood ❤
@santoshb7776
@santoshb7776 Жыл бұрын
Understood sir 🙏🙏
@vivekkumarsingh178
@vivekkumarsingh178 Ай бұрын
Why we cannot optimise space when their was 2 for loops and just by removing one we are doing same thingh 😢???
@atheisth2373
@atheisth2373 Жыл бұрын
Thanks a lot...Striveerr
@rishabmalhotra6980
@rishabmalhotra6980 2 жыл бұрын
I tried to space optimize it by mytself for 6 variables and I guess i fucked my brain so much that i started questioning my existence. You explained it so well thanks strive.
@uditgarg6508
@uditgarg6508 5 ай бұрын
put k = n/2 and ind+2 in previous , runs fine
@divakarv3303
@divakarv3303 2 жыл бұрын
Space Optimisation using 4 variables and 4 lines of code int a=0,b=0,c=0,z=0; for(int i=prices.length-1;i>=0;i--){ int x=z; a=Math.max( -prices[i] + b, a); b=Math.max( prices[i] + c , b); c=x; z=a; } return a; Thanks a lot striver for Graph,Tree and DP series
@akashsahu2571
@akashsahu2571 Жыл бұрын
yes
@bibs24
@bibs24 Жыл бұрын
Thank you
@LBK3
@LBK3 Жыл бұрын
understood ❤
@Harsh-jc2bz
@Harsh-jc2bz 2 ай бұрын
diid it by myself
@dum0
@dum0 3 ай бұрын
damn i cant believe , i solved this problem under few seconds only listening question , actually getting gud
@shivambajpeyi9008
@shivambajpeyi9008 2 жыл бұрын
Understood, did this one by myself hehe
@aditya14-02
@aditya14-02 3 ай бұрын
Thanks!
@nihalsingh6233
@nihalsingh6233 Жыл бұрын
Understood
@himanshuagrawal8012
@himanshuagrawal8012 2 жыл бұрын
Thanku so much Raj bhaiya for DP videos , now i m feeling very much confident in DP, I can now teach to the interviewer as well 😜😜😅😅 just_kidding #UNDERSTOOOODDDDDDDD
@Himani-t3g
@Himani-t3g 21 күн бұрын
understood!
@ratinderpalsingh5909
@ratinderpalsingh5909 Жыл бұрын
Understood, sir. Thank you very much.
@abdalla4425
@abdalla4425 11 ай бұрын
Understood!
@asheshkaran5843
@asheshkaran5843 Жыл бұрын
UNDERSTOOD!!!!!
@HarshKumar-ip5nr
@HarshKumar-ip5nr Жыл бұрын
I have solved this question using 3d DP. Is this solution ok for the interview?
@googleit2490
@googleit2490 Жыл бұрын
DP revision: Was applying some other complex logic, had to watch the video upto 5:28 Nov'18 2023 02:51 pm
@rishabhagarwal8049
@rishabhagarwal8049 2 жыл бұрын
Understood Sir, Thank you very much
@xpyder
@xpyder 2 жыл бұрын
I think you can space optimize this easily by using Mod 3 on your array indexes (dp[ (ind+1)%3 ][0] and dp[ (ind+2)%3 ][1], etc), is this correct?
@pusarlaaishwarya5035
@pusarlaaishwarya5035 2 жыл бұрын
Canyou explain this condition please?
@me.deepaksharma
@me.deepaksharma 2 жыл бұрын
Thank You :)
@keen_kartik
@keen_kartik Жыл бұрын
This man is genius
@kumarpurushottam632
@kumarpurushottam632 Жыл бұрын
Thanks a Lot Striver : )
@adityasaxena6971
@adityasaxena6971 Жыл бұрын
Thanks Striver
@willastralian7141
@willastralian7141 Жыл бұрын
WHAT IS IT WITH THESE FUCKING EXCELLENT INDIAN TUTORS DUDE. CAN'T BECOME A SUPER POWER QUICK ENOUGH
@Hello-ro6hr
@Hello-ro6hr 2 жыл бұрын
UNDERSTOOD ... Ideally at 15.09 it should be front1[1]...
@VivekGawande1
@VivekGawande1 Жыл бұрын
Great explanation!
@hemantpatel1413
@hemantpatel1413 Жыл бұрын
understood.
@parvahuja7618
@parvahuja7618 6 ай бұрын
thankyou
@kartikrameshchavan6662
@kartikrameshchavan6662 Жыл бұрын
Understood 🙏
@sauravchandra10
@sauravchandra10 Жыл бұрын
Understood, thanks!
@jivanninawe3190
@jivanninawe3190 2 жыл бұрын
Striver bhahiya LIS pr dp banaho na bhaut question ke sath lis nahi samja muje
@verma_jay
@verma_jay Жыл бұрын
understood
@ajitzote6103
@ajitzote6103 2 жыл бұрын
understood!
@rishabhraj8233
@rishabhraj8233 10 ай бұрын
solved without watching 😸👽
@pankajmittal1002
@pankajmittal1002 6 ай бұрын
understood!!
@mriduljain6809
@mriduljain6809 Жыл бұрын
Understood Bhaiya..
@parthsalat
@parthsalat 2 жыл бұрын
Understood kaka
@tasneemayham974
@tasneemayham974 Жыл бұрын
DONE!!
@Professor-du2pf
@Professor-du2pf 10 ай бұрын
"US"
@aditithakur6226
@aditithakur6226 Жыл бұрын
Understood Sir.
@VikashYadav-px8ei
@VikashYadav-px8ei Жыл бұрын
Understood 🎉
@gangsta_coder_12
@gangsta_coder_12 2 жыл бұрын
Understood 💯💯💯
@venkateshvenky2880
@venkateshvenky2880 2 жыл бұрын
#understood
@vaishnavithakur6460
@vaishnavithakur6460 2 жыл бұрын
Understood so very well!!!!
@gaboja__goo
@gaboja__goo Жыл бұрын
thx very much.
@parshchoradia9909
@parshchoradia9909 Жыл бұрын
Understood Sir!
@sameersahu4569
@sameersahu4569 2 жыл бұрын
Understood!!!Thank you
@nimmalavishnu3044
@nimmalavishnu3044 2 жыл бұрын
Superb explaination striver
@dheerajshukla7008
@dheerajshukla7008 5 ай бұрын
thats amazing
@gurubhargava6950
@gurubhargava6950 2 жыл бұрын
Understood!!
@gentleman7060
@gentleman7060 Жыл бұрын
In any videos of buy and sell stock u didnt mention overlapping. Why?
@oblivion_5910
@oblivion_5910 2 жыл бұрын
understood
@THEAMITESHRANJAN
@THEAMITESHRANJAN Ай бұрын
"us"
@urvashi2410
@urvashi2410 Жыл бұрын
This was so good!!
@shubhigupta5689
@shubhigupta5689 2 жыл бұрын
Understood🌻
@curiouscoder9566
@curiouscoder9566 Жыл бұрын
great content
@satyampande684
@satyampande684 2 жыл бұрын
understood!!
@Harshit126
@Harshit126 2 жыл бұрын
Understood, thanks
@rohalkurup1350
@rohalkurup1350 2 жыл бұрын
Understood !!!!
@gopalaggarwal9649
@gopalaggarwal9649 Жыл бұрын
US
@suheabkhan2546
@suheabkhan2546 2 жыл бұрын
understood!!!!!!!!
@calderanoadi5982
@calderanoadi5982 2 жыл бұрын
UNDERSTOOD
@vader9782
@vader9782 Ай бұрын
well i did this i created an extra space for cooldown in the vector class Solution { public: int maxProfit(vector& prices) { int n=prices.size(); vector curr(3,0),next(3,0); next[0]=prices[n-1]; for(int i=n-2;i>=0;i--) { for(int buypt=2;buypt>=0;buypt--) { int profit=0; if(buypt==2) profit =next[1]; else if(buypt==1) { int buy=next[0]-prices[i]; int hold=next[1]; profit = max(buy,hold); } else { int sell=prices[i]+next[2]; int hold=next[0]; profit = max(sell,hold); } curr[buypt]=profit; } next=curr; } return next[1]; } };
@priyanshvatsal9791
@priyanshvatsal9791 Жыл бұрын
Understood 😇😊
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
HARD_MMA
Рет қаралды 3,3 МЛН
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 9 МЛН
DP 36. Buy and Sell Stock - II | Recursion to Space Optimisation
35:34
take U forward
Рет қаралды 249 М.
Best time to buy and sell stock with cooldown | Leetcode #309
34:31
DP 37. Buy and Sell Stocks III | Recursion to Space Optimisation
31:50
take U forward
Рет қаралды 178 М.
Buy/Sell Stock With K transactions To Maximize Profit Dynamic Programming
29:09
Tushar Roy - Coding Made Simple
Рет қаралды 177 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Best Time to Buy and Sell Stock III | Leetcode #123
35:40
Techdose
Рет қаралды 71 М.
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
HARD_MMA
Рет қаралды 3,3 МЛН