Best Time to Buy and Sell Stock III | Leetcode

  Рет қаралды 72,249

Techdose

Techdose

Күн бұрын

Пікірлер: 183
@faizanusmani2200
@faizanusmani2200 3 жыл бұрын
Subscribed right away. Cant imagine the amount of work this creator is putting in making these videos. Hats Off.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@dhruvsharma6772
@dhruvsharma6772 4 жыл бұрын
Thank you very much for such a wonderful explanation.Intuition development is very Important rather than coding and learning formula for this problem and you have explained it in depth with appropriate examples. Please keep making videos like these.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@MrRobot-mb6rq
@MrRobot-mb6rq 3 жыл бұрын
Why KZbin doesn't have option for liking more than once? Great explanation man🔥
@techdose4u
@techdose4u 3 жыл бұрын
Thanks : )
@nightmarauder2909
@nightmarauder2909 3 жыл бұрын
I really like that you explained the thought process behind implementing memoization. Thank you for that!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😊
@rajatbudania6181
@rajatbudania6181 4 жыл бұрын
Best explanation series on Buy And Sell Problems !!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@gurnoorsingh2954
@gurnoorsingh2954 4 жыл бұрын
I don't think this could have been explained better than you did
@techdose4u
@techdose4u 4 жыл бұрын
Thanks buddy :)
@AkshayKumar-xh2ob
@AkshayKumar-xh2ob 3 жыл бұрын
Was struggling with this question and the intuition behind the soln for the whole day. As always TECH DOSE saves the day.
@mohinipotdar477
@mohinipotdar477 3 жыл бұрын
Explained very well and smoothly converted complex problem into simpler one. Thanks for such wonderful video.
@tanvisharma6879
@tanvisharma6879 3 жыл бұрын
Very well explained. Hats off to Tech Dose!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@dushyantsingh2624
@dushyantsingh2624 4 жыл бұрын
watching his amazing explanation at 3:20 am while waiting for 4:20am
@techdose4u
@techdose4u 4 жыл бұрын
Wow 😅
@lapujain
@lapujain 4 жыл бұрын
Amazing explanation. Hats off to you for putting so much efforts.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@annawilson3824
@annawilson3824 3 жыл бұрын
Awesome video! In principle, we don't need the 1st loop, and going further we can solve even in one scan.
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@subhascse
@subhascse 2 жыл бұрын
Wonderful!! Finally a very good explanation!! Thanks a lot.
@jaatharsh
@jaatharsh 4 жыл бұрын
thanks a lot, dude, ur detailed explanation definitely helped understand the crux of the problem. keep up the great work.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@rajeshadam978
@rajeshadam978 3 жыл бұрын
I think its somewhat complicated to explain but you explained well ! here is a easy approach : class Solution { public int maxProfit(int[] p) { int pr1=0; int pr2=0; int mn1=Integer.MAX_VALUE; int mn2=Integer.MAX_VALUE; for(int i=0;i
@vrushankrao4911
@vrushankrao4911 4 жыл бұрын
Kicking off the day with this video 🔥
@techdose4u
@techdose4u 4 жыл бұрын
Nice :)
@bharathkumar5870
@bharathkumar5870 2 жыл бұрын
wow..that divide and conquour approach is super
@MrACrazyHobo
@MrACrazyHobo 3 жыл бұрын
For any newcomers, the divide and conquer approach is the easiest to understand.
@BurhanWani1
@BurhanWani1 4 жыл бұрын
Great explanation. Keep up the good work
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@abhilakshmaheshwari9360
@abhilakshmaheshwari9360 2 жыл бұрын
Big cheers to you man, explained really well!!!!🙌
@MiddleClassTalk
@MiddleClassTalk 2 жыл бұрын
amazing devide and conqure approach
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😄
@saneethvarma7806
@saneethvarma7806 4 жыл бұрын
Best Explanation for this problem.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@MrKasle
@MrKasle 3 жыл бұрын
Great Explanation!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@amchourasia
@amchourasia 3 жыл бұрын
Beautiful explanation
@kuldeepdebnath4787
@kuldeepdebnath4787 3 ай бұрын
Great Explanation! Really helpful to understand.
@techdose4u
@techdose4u 3 ай бұрын
Nice :)
@EditorGuru
@EditorGuru 3 жыл бұрын
Amazing explanation brother...Thank you so much
@ajaywadhwa3398
@ajaywadhwa3398 4 жыл бұрын
Wow !!! This is really an awesome explanation.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@Suraj-lo7bg
@Suraj-lo7bg 2 жыл бұрын
i should take max(profit, left[i]+right[i]) . if we take left[i-1] we're excluding a fact that ith element may be the maximum of left subarray and minimum of right subarray.... in that case. transaction where we sell on ith day while we bought from a day on left, is ignored. and if we have to sell and buy on same day... that's same as holding it, because left subarray ensures that we've already purchased before we're trying to sell. check for the input.. 1 2 3 4 5 . now consider 3 as pivot.. profit left = 1, and profit right = 5-3 = 2.. total profit = 3... but max profit is 4. 5-1.
@pavittarkumarazad3259
@pavittarkumarazad3259 8 ай бұрын
This needs to be pinned. I was facing the same issue and resolved this by myself but was looking for the explanation. Thanks :)
@anmolsharma9539
@anmolsharma9539 Жыл бұрын
This is some next level stuff thank you so much sir. you are the real deal.
@vishalverma920
@vishalverma920 2 жыл бұрын
what is the time complexity of recursive solution after memoization ??
@manasbohat6883
@manasbohat6883 4 жыл бұрын
thank you man , this was amazing
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@JyotiprakashMandal-bp8ng
@JyotiprakashMandal-bp8ng 5 ай бұрын
Divide and conquer easy damn easy thanks a lot
@SHASHIKUMAR-pp4hg
@SHASHIKUMAR-pp4hg 4 жыл бұрын
I did it in O(n) time and space still 5%,5% on leetcode!!!!!!!!!!!!!!
@pamelamoore9608
@pamelamoore9608 3 жыл бұрын
Don't feel bad ... I tried a bottom-up DP approach to this, allocating a jagged array which is essentially O((N^2)/2) space (each cell storing an int), where each row corresponds to the end of the subarray and the column corresponds to the beginning of the subarray, and I also approach this using a divide and conquer (i.e. partition the array in 2 pieces (for the 2 transactions), and calculate max profit per subarray (calculating just once), and I don't get any TLE, but I blow up memory about test case 202 (of 214). I'm now trying to figure out how I can calculate better the next partition based on the previous partition. It's vicious (that's why it's hard).
@robertmiller3765
@robertmiller3765 Жыл бұрын
I think the string operation glossed over in the middle is actually O(1) (since the length of the string would be constant) as it's just concatenating 3 numbers (not n numbers).
@santoshreddy3
@santoshreddy3 4 жыл бұрын
You are awesome. Very nice explanation.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@siddharthmehta6966
@siddharthmehta6966 2 жыл бұрын
Simply , Loved it !
@BlumChoi
@BlumChoi 2 жыл бұрын
Please explain how to choose the dividing line in the divide and conquer approach
@hriteshupadhyaya4440
@hriteshupadhyaya4440 4 жыл бұрын
your dp approach is not getting accepted on interviewbit.
@techdose4u
@techdose4u 4 жыл бұрын
Maybe they want divide and conquer. Interviewbit may set their requirement for the most optimal. Try the same on leetcode with DP. It depends of what complexity they are expecting.
@hriteshupadhyaya4440
@hriteshupadhyaya4440 4 жыл бұрын
@@techdose4u ya It is fine for leetcode
@kapilgupta8543
@kapilgupta8543 3 жыл бұрын
Nice intuitive explanation!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@vinayak186f3
@vinayak186f3 3 жыл бұрын
If you don't come up with the solution , ask the interviewer to explain it to you
@techdose4u
@techdose4u 3 жыл бұрын
😂
@nikhilchandna4851
@nikhilchandna4851 4 жыл бұрын
Great Explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@meylyssa3666
@meylyssa3666 4 жыл бұрын
Nice explanation! Thanks!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@seekimaan
@seekimaan Жыл бұрын
amazing explanation, thank you very much sir!
@sumashreyatv2506
@sumashreyatv2506 4 жыл бұрын
Amazing explanation....so easy to understand.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@praveenbandaru8700
@praveenbandaru8700 2 жыл бұрын
why is time complexity 2^n for memoization?
@Samir-ow6sq
@Samir-ow6sq 2 жыл бұрын
Good Explanation
@mystryb345
@mystryb345 4 жыл бұрын
very good explanation sir
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@MansiThakkar139
@MansiThakkar139 3 жыл бұрын
Very useful! Thank you
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@rahulmistry9173
@rahulmistry9173 3 жыл бұрын
Amazing!!!! Could you explain the time complexity after adding memoization to the recursive solution? You didn't mention that
@MrVishal203
@MrVishal203 3 жыл бұрын
I think, number of calls in that case will also be 2^n, only difference is it will not calculate all the cases as the repeated ones are already stored. Please correct if this is wrong.
@ashutoshtiwari4719
@ashutoshtiwari4719 3 жыл бұрын
Thank you !!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@dovyraj1272
@dovyraj1272 4 жыл бұрын
sir, nice explanation, but how to get idea , the idea is not striking at all, when i see a new kind of problem, please help
@techdose4u
@techdose4u 4 жыл бұрын
Maybe divide and conquer don't strike but recursion should be intuitive if you had solved cooldown problem or if you have practiced enough on backtracking. So try to think about observations and try to see if a problem can be framed or converted to other problem. This was you can practice to improve yourself.
@Mandeepsingh-jo5cf
@Mandeepsingh-jo5cf 3 жыл бұрын
You are the Best .
@vinothrajg8576
@vinothrajg8576 4 жыл бұрын
Great stuff again ☺️.. Thank your
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@drishtdyumnshrivastava5313
@drishtdyumnshrivastava5313 2 жыл бұрын
that divide and conquer approach simultaneously gave me Goosebumps but also left me in awe by the pure beauty of the approach..... As always really appreciate your work.....thank you.!!!
@manishgoyal5298
@manishgoyal5298 4 жыл бұрын
Can you please suggest some more problems to practice peak valley approach.
@techdose4u
@techdose4u 4 жыл бұрын
Other versions of this problem are on similar logic. I had covered more of such videos in challenges but don't remember the name 😅
@yitingg7942
@yitingg7942 3 жыл бұрын
Wow you just never stop surprising me. I am speechless!! Can I be your student please ?!!!😉😉
@techdose4u
@techdose4u 3 жыл бұрын
You are not just a student 🤪
@SHASHANKRUSTAGII
@SHASHANKRUSTAGII 3 жыл бұрын
Just tell me how will it come to my mind? How can one think this optimal approach by own?
@aksay.3823
@aksay.3823 3 жыл бұрын
The idea(Divide and conquer one) is kind of related to the candy peoblem in leetcode.
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@aksay.3823
@aksay.3823 3 жыл бұрын
@@techdose4u Hey how about when we generalize and say k transactions are allowed then we will have to apply the memoization approach?
@ajmalmohamed
@ajmalmohamed 3 жыл бұрын
Awesome
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@abhishekkumargupta3043
@abhishekkumargupta3043 4 жыл бұрын
Can't we sell and then buy that same stock on the same day? Or, are we already taking that into account here in the mentioned process. Please explain.
@techdose4u
@techdose4u 4 жыл бұрын
We can't sell before buying and we cannot perform multiple operations on the same day. Only buy or sell on a day. I had said about this in the video.
@abhishekkumargupta3043
@abhishekkumargupta3043 4 жыл бұрын
@@techdose4u Yes seen that, but it's not mentioned in the given problem description. Apparently ,we have to assume that. Well thanks for the reply. Cheers!
@techdose4u
@techdose4u 4 жыл бұрын
Yea it was not clear.
@gladyouseen8160
@gladyouseen8160 3 жыл бұрын
Sir can we solve this problem using state machine?
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
Sir really very amazing 😍😊 video and explanation is seriously beautiful and 😍😍😍
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@vaibhavaggarwal6808
@vaibhavaggarwal6808 3 жыл бұрын
is any iterative solution possible
@hoyinli7462
@hoyinli7462 3 жыл бұрын
thx for ur video!
@shaswatdas6553
@shaswatdas6553 4 жыл бұрын
You should upload on Box stacking DP problem. No good content is available on this topic. Thanks in advance!! ❤️
@techdose4u
@techdose4u 4 жыл бұрын
Okay I will try
@shaswatdas6553
@shaswatdas6553 4 жыл бұрын
@@techdose4u thanks!❤️ Can you do it sooner?😅 Interview date is near.
@xiaoweidu4667
@xiaoweidu4667 10 ай бұрын
Fantastic!
@chaitanyabalanagu626
@chaitanyabalanagu626 Жыл бұрын
divide and conquer @ 14:16
@PawanKumar-tu6ti
@PawanKumar-tu6ti 2 жыл бұрын
thank you sir.
@tilakrajrao2753
@tilakrajrao2753 4 жыл бұрын
what will the overall time complexity of memoization approach ??? please do reply
@toose8388
@toose8388 2 жыл бұрын
Isn't this divide and conquer approach O(n^2)? As you have to loop over each element, and for each element check all elements to its left and right? In fact, my implementation of this method exceeds time limit on leetcode...
@prateeksinghal630
@prateeksinghal630 4 жыл бұрын
Can you please tell the time complexity of the memoized solution for this problem !!
@seal0118
@seal0118 4 жыл бұрын
how did you know all this, sir? is there a book that teaches you how to tackle these problems ?
@techdose4u
@techdose4u 4 жыл бұрын
There is no such book 😅
@seal0118
@seal0118 4 жыл бұрын
​@@techdose4u but sir, this is very unintuitive for me, how do i learn about all this?, this is the first time i see dp problem with more than 1 state
@souravsaha3446
@souravsaha3446 3 жыл бұрын
Nice sir but in real practice we can sell a stock before buying .Thank you for amazing explanation
@VS-cy8oy
@VS-cy8oy 4 жыл бұрын
I have doubt, how do we come up with divide and conquer approach. i mean, How do we come to know that create left and right array with values and summing up them will give us the result.Is there any logical sequence behind this summing technique, from left to right i understood, but from right to left i am unable to get on what ground we are confirmed that taking max will work. I am confused in that, Will be huge help if you can clear this doubt please. I have solved it with recursion, but that wasnt accepting on IB, so i came across your d&c approach, please clear this doubt, thanks and really appreciate your work..
@SushilKumar-wt7js
@SushilKumar-wt7js 4 жыл бұрын
in memoization case time complexity would be O(2*3*n) => O(n) right?
@agnesakne4409
@agnesakne4409 2 жыл бұрын
I don't know
@tharunkumarreddy1224
@tharunkumarreddy1224 2 жыл бұрын
could someone explain difference between buy and sell stock2 problem and buy and sell stock 3 problem
@utkarshpanwar2358
@utkarshpanwar2358 4 жыл бұрын
how can i see the implemented class behind the function on leetcode . please help me
@techdose4u
@techdose4u 4 жыл бұрын
I don't think you can.
@utkarshpanwar2358
@utkarshpanwar2358 4 жыл бұрын
@@techdose4u but i did many times now i forgot how i did that or may they removed that option
@utkarshpanwar2358
@utkarshpanwar2358 4 жыл бұрын
@@techdose4u you are doing good may i suggest you something which i feel you should work on that related to presentation
@vivekbudge9706
@vivekbudge9706 4 жыл бұрын
@@utkarshpanwar2358 check successfully submitted solution, there are bars....for all usecases... click b on bar ... it will show underline program
@golekhabhoi7356
@golekhabhoi7356 2 жыл бұрын
After buying a stock fall in - 5% what is the calculatin of profit and loss kingly guide me
@jimsmith4739
@jimsmith4739 4 жыл бұрын
@techdose Why are we going from end to middle in case of the right side from center and why not go just like he left side which is (along the timeline and not against)
@Arunkumar-bo2wc
@Arunkumar-bo2wc 2 жыл бұрын
Amazing
@aleaf355
@aleaf355 Жыл бұрын
Please add timestamps for long videos
@techdose4u
@techdose4u Жыл бұрын
Sure
@witty_idipt
@witty_idipt 4 жыл бұрын
can you please tell how time complexity of state machine code will be O(n)
@Kevin-gf8mq
@Kevin-gf8mq 4 жыл бұрын
Why do we need 3 states? At end of the day, there're only 2 states anyway, either hold cash or hold stock.
@yashjain992
@yashjain992 4 жыл бұрын
This is gold!!
@techdose4u
@techdose4u 4 жыл бұрын
😁
@abhishekkumargupta3043
@abhishekkumargupta3043 4 жыл бұрын
why going backwards works?
@abhishekkumargupta3043
@abhishekkumargupta3043 4 жыл бұрын
Ok, so i understand it. (* starts crying in background).
@techdose4u
@techdose4u 4 жыл бұрын
??
@abhishekkumargupta3043
@abhishekkumargupta3043 4 жыл бұрын
@@techdose4u why going backwards (i.e. selling first and buying later) or reversing timeline for 2nd array works?
@ankushroy919
@ankushroy919 4 жыл бұрын
i dont understand why u are moving in opposite direction we can also move from the i th position towards nth position
@mhaesh
@mhaesh 4 жыл бұрын
Nice explanation. Can you please make video for Best Time to Buy and Sell Stock with transaction fee
@MagicalCreationAviCreation
@MagicalCreationAviCreation 4 жыл бұрын
Sir I am solving leetcode challenges since from 1 and half month , but some of problem I can't able to solve by my own ,once I watch solution then I think that I don't know even simple logic also, then i feel sad, then i close the laptop, sir can you give me any suggestions I want to become like u sir,
@techdose4u
@techdose4u 4 жыл бұрын
Just spend some time about thinking the problem. Try harder each time. If you can't solve then read editorials and try to code yourself. You will definitely improve :)
@MagicalCreationAviCreation
@MagicalCreationAviCreation 4 жыл бұрын
@@techdose4u yeah I will definitely do sir,thank you sir
@ayushgarg5929
@ayushgarg5929 4 жыл бұрын
Shortest Code Ever:- int maxProfit(vector& prices) { int n=prices.size(); if(n==0) return 0; int k1=0,k2=0,b1=-prices[0],b2=-prices[0],K1,K2,B1,B2; for(int i=1;i
@shaheenrafiq5974
@shaheenrafiq5974 3 жыл бұрын
It's almost like you've hacked the human brain. *_* Cant believe I understood it in a single go!
@starc701
@starc701 4 жыл бұрын
sir one request from deepest of my heart ,and soul . can u make its 4th part, which i consider the toughest among all. plz sir , plz i beg u.plz make it , placement session is going on.
@techdose4u
@techdose4u 4 жыл бұрын
Okay I will make it.
@starc701
@starc701 4 жыл бұрын
@@techdose4u and sir in ur telegram channel u said to ask questions there, but how can we message in a channel, telegram doesn't allow that, plz create a group sir, thank you very much sir
@techdose4u
@techdose4u 4 жыл бұрын
I have created both group and channel. Join both. They have different uses.
@starc701
@starc701 4 жыл бұрын
@@techdose4u oops i only saw the channel link, now i got it.
@ArbitCode
@ArbitCode 4 жыл бұрын
which software do you use sir? is it openboard
@techdose4u
@techdose4u 4 жыл бұрын
Wacom pro sketch.io
@thinkverse94
@thinkverse94 4 жыл бұрын
One Query. Please Clarify @TECH DOSE for example, prices = [1,2,3,4] If we partition in price=2, ([1,2] [2,4]) OR ([1,2] [3,4]) ...which one is correct? My question is, in the same day can we sell the stock first and buy the stock ? (i.e. [[1,2] [2,4]] / [[1,3][3,4]] / ...... ) Or, Sell and buy must be in diff day ?
@sangeethm1984
@sangeethm1984 Ай бұрын
irst_buy = float('inf') first_profit = 0 second_buy = float('inf') second_profit = 0 for price in prices: first_buy = min(first_buy, price) first_profit = max(first_profit, price - first_buy) second_buy = min(second_buy, price - first_profit) second_profit = max(second_profit, price - second_buy) return second_profit
@SVW_SHA
@SVW_SHA 4 жыл бұрын
Would you please add subtitle in your video?
@techdose4u
@techdose4u 4 жыл бұрын
I am trying. I hope it's fixed soon. I am getting some problem in auto-generated English sub.
@ashishg656
@ashishg656 4 жыл бұрын
Great :)
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@nimishabajpai6816
@nimishabajpai6816 4 жыл бұрын
Can anyone tell that in the preferred language if I have wriitten java in kick start competition then I will get only java to use in coding??
@exodus5948
@exodus5948 4 жыл бұрын
Legends come at night 😁😁
@techdose4u
@techdose4u 4 жыл бұрын
😂
@2121sourabh
@2121sourabh 3 жыл бұрын
GOD
@techdose4u
@techdose4u 3 жыл бұрын
😅
@AshwaniKumar-tw8vp
@AshwaniKumar-tw8vp 3 жыл бұрын
𝙄 𝙖𝙢 𝙖𝙙𝙙𝙞𝙘𝙩𝙚𝙙 𝙩𝙤 𝙙𝙤𝙨𝙚𝙨 𝙜𝙞𝙫𝙚𝙣 𝙗𝙮 𝙏𝙀𝘾𝙃 𝘿𝙊𝙎𝙀.🔥🔥🔥🔥
Best time to buy and sell stock 4 | Leetcode #188
32:22
Techdose
Рет қаралды 26 М.
Air Sigma Girl #sigma
0:32
Jin and Hattie
Рет қаралды 45 МЛН
DP 37. Buy and Sell Stocks III | Recursion to Space Optimisation
31:50
take U forward
Рет қаралды 186 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 238 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 731 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 147 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 384 М.
DP 36. Buy and Sell Stock - II | Recursion to Space Optimisation
35:34
take U forward
Рет қаралды 262 М.