Best Time to Buy and Sell Stock III | Leetcode #123

preview_player
Показать описание
This video explains a very important programming interview problem which is buy and sell stock 3 which is from leetcode 123.I have already explained buy and sell stock 2 and buy and sell stock with cooldown and the LINK for these are present below.In this problem, i have explained the solution along with intuition for solving the problem.I have first shown the solution using backtracking and recursion and then i have shown the optimization using multi state memoization array.I have also shown an alternate memoization technique which is by using key as string (containing all appended states) and value as maximum profit obtained for that state.I have also shown all possible state transitions for all possible choices.The second method is by using divide and conquer technique which is intuitive if we divide the entire stock days into 2 parts and we take the maximum profit from each part and just add them to get max profit.I have shown intuition with examples for solving using divide and conquer.At the end of the video,I have explained the code walk through for both the techniques.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

========================================================================
Join this channel to get access to perks:

=======================================================================

USEFUL VIDEOS:-
Рекомендации по теме
Комментарии
Автор

Subscribed right away. Cant imagine the amount of work this creator is putting in making these videos. Hats Off.

faizanusmani
Автор

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<p.length;i++){
mn1=Math.min(p[i], mn1);
pr1=Math.max(pr1, p[i]-mn1);
mn2=Math.min(mn2, p[i]-pr1);
pr2=Math.max(pr2, p[i]-mn2);
}
return pr2;
}
}

rajeshadam
Автор

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.

dhruvsharma
Автор

I really like that you explained the thought process behind implementing memoization. Thank you for that!

nightmarauder
Автор

Best explanation series on Buy And Sell Problems !!

rajatbudania
Автор

Awesome video! In principle, we don't need the 1st loop, and going further we can solve even in one scan.

annawilson
Автор

I don't think this could have been explained better than you did

gurnoorsingh
Автор

The whole point about memoization array vs map around 14:00 is incrorrect. The amount of space still remains the same. How? Calculate the total possible values for the map and the size of your array.

_rmw
Автор

Explained very well and smoothly converted complex problem into simpler one. Thanks for such wonderful video.

mohinipotdar
Автор

For any newcomers, the divide and conquer approach is the easiest to understand.

MrACrazyHobo
Автор

Was struggling with this question and the intuition behind the soln for the whole day. As always TECH DOSE saves the day.

AkshayKumar-xhob
Автор

Why YouTube doesn't have option for liking more than once?
Great explanation man🔥

MrRobot-mbrq
Автор

Very well explained. Hats off to Tech Dose!

tanvisharma
Автор

thanks a lot, dude, ur detailed explanation definitely helped understand the crux of the problem.
keep up the great work.

jaatharsh
Автор

Amazing explanation. Hats off to you for putting so much efforts.

lapujain
Автор

Wonderful!! Finally a very good explanation!! Thanks a lot.

subhascse
Автор

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).

robertmiller
Автор

watching his amazing explanation at 3:20 am while waiting for 4:20am

dushyantsingh
Автор

Big cheers to you man, explained really well!!!!🙌

abhilakshmaheshwari
Автор

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.

Suraj-lobg
visit shbcf.ru