leetcode leetcode python leetcode solutions how to use leetcode leetcode java leetcode python solutions leetcode problems how to study leetcode how to use leetcode effectively leetcode java solutions python leetcode leetcode preparation leeetcode problems leetcode coding solutions leetcode 13 leetcode 45 is leetcode premium worth it leetcode 201 leetcode #13 leetcode tips leetcode #200 leetcode 1886 leetcode 1887 leetcode 1889 leetcode leetcode 122 leetcode solutions leetcode problems best time to buy and sell stock leetcode best time to buy and sell stock ii leetcode leetcode python eric programming leetcode 122 leetcode 121 leetcode 30 day challenge leetcode 122 java leetcode 122 leetcode 121 leetcode 122 best time to buy and sell stock ii buy and sell stocks leetcode leetcode 309 leetcode java leetcode easy leetcode 2021 april 5 leetcode leetcode tutorial best time to buy and sell stock ii maximum profit by buying and selling a share at most twice anuj bhaiya best time to buy and sell stock leetcode stock buy and sell 122. best time to buy and sell stock ii best time to buy and sell stock iii leetcode 122 stock buy and sell leetcode best time to buy and sell stock buy and sell stocks leetcode best time to buy and sell stock ii leetcode stock buy and sell gfg stock buy and sell problem buy and sell stock leetcode stock span problem stock buy sell to maximize profit maximum profit stock buy and sell problem leetcode dsa buy and sell stock 2 leetcode best time to buy stocks leetcode 121. best time to buy and sell stock best time to buy and sell stock leetcode ii buy and sell buy and sell stocks 2 maximum profit by buying and selling a share atmost twice maximum profit gfg best time to buy and sell stock 2 leetcode best time to buy and sell stock with transaction fee dsa one 123. best time to buy and sell stock iii best time to buy and sell stock 2 best time to buy and sell stock with cooldown buy and sell stocks buy sell stock leetcode love babbar profit 2 day best time to buy and sell stock iv best time to buy and sell stock java best time to sell stocks leetcode 2 max profit leetcode maximum profit by buying and selling share at most twice new movies 2021 stock buy sell stock buy sell leetcode stock maximize when to buy and sell stocks accounts merge leetcode anuj bhai anuj bhaiya dsa anuj bhaiya resume best time to buy and sell stock leetcode java best time to buy and sell stocks 3 best time to buy stocks leetcode best time to sell and buy stock leetcode best time to sell stocks leetcode bit manipulation buy and sell stock course schedule leetcode dil leke darde dil de gaye dsa anuj dsa anuj bhaiya dsa one course heap leetcode 121 leetcode 123 leetcode 421 leetcode best time to buy and sell stock ii leetcode solutions maximize profit hackerrank solution maximum profit by buying and selling shares at most twice plus one leetcode rachit jain realme wireless pro stock buy sell problem stock profit stock to buy string compression tapping rain water problem two city scheduling leetcode
Content
0 -> Hello Whats up guys, Anuj here and welcome to the DSA 1 course
2.352 -> And today in this video we are going to talk about buy and sell stock part-2
5.812 -> In the last video we had seen part 1, in which we could buy stock only one time and sell it
10.994 -> In today's problem we can buy and sell stock how much ever time we want
15.316 -> So we can do buy operation and sell operation as much we wish to
18.165 -> And we have to maximize our profit
19.976 -> There is a catch in this, which is, after every buy you have to sell
24.299 -> This means you can't buy continuously 2 times, that means at a time you should have only 1 stock, you cannot purchase 2 stocks
30.986 -> So, basically this is the problem, that again you are given an array
34.787 -> And in this array, it is specified that on each day what is the price of stock
37.828 -> So, there is one stock only, whose multiple are already given, you already know the future price
43.055 -> And you can buy and sell any number of stocks, but you should always have 1 stock
48.317 -> It is not possible that you have 2 stocks at a time
51.043 -> This means you have to sell immediately after buy
53.229 -> So how the profit will maximize?
54.659 -> Suppose , we purchased on the day with price 2, and sold on the day with price 7, so this can be 1 transaction
60.94 -> Next transaction can be we purchased on the day with price 3, and sold on the day with price 6
63.976 -> So this can be our transaction, and what will be the next transaction?
67.041 -> We purchased on the day with price 1, and sold on the dat with price 4
69.747 -> So from here what profit are we getting?
71.49 -> We get 7-2=5., 3-6=3, 4-1=3
76.787 -> If we add them 5+3+3=11
78.747 -> So from here the maximum profit we get from here is 11
81.581 -> We will not get more profit then this from here
83.172 -> So this the problem and now I think you might have already understood from here that how we have to solve it
89.933 -> But still think how can we solve it
91.495 -> And after that we will move ahead
92.997 -> So I have represented the same thing in a graphical form, so that you can clearly understand the proposed solution which I will tell you all
99.404 -> So basically, what we will do is, that whenever the bottom will come we will sell it, and whenever it will be at peak we will purchase it
106.466 -> So, I think you might have understood from here only that how it will be done
110.309 -> So whenever we are getting a local minimum, when ever we are getting local minimum of stock price, we will purchase the stock
116.228 -> And as we will come to know that the peak has came, we will sell it
119.294 -> And this the ideal situation of any stock market, that when the market will dip, when the market is down, at that time we purchase the stocks
128.874 -> And when the market is at the top, when it is going to crash, we sell it
132.815 -> But we don't know about future that when it will be at bottom and when it will be at top
136.633 -> So that is why over here, if you know the past trend in stock market, then it is more helpful
143.273 -> And there are many other factors also
145.786 -> But over here we will come out of it, we will come back to our problem
148.713 -> So, when our market will be down we will purchase it, and when it will be on top we will sell it
154.305 -> So we purchased at 2, sold at 7
155.714 -> So we got the profit of 5
157.194 -> So from this one we have got the profit 5
160.838 -> Then next local minimum is this, when will be the local minimum, when the left and right prices are bigger than the current price
166.598 -> This is the way to identify the local minimum
168.261 -> What is the way to identify the local minimum that, the left one and right one both are greater
171.159 -> And what is the identification of local maximum?
173.101 -> That the left one is also smaller and the right one is also smaller
174.748 -> So we purchased at 3 and sold at 6, because 6 is local maximum, how?
177.835 -> Because left price from 6 is smaller and the price on right side of 6 is also smaller, 3 and 1
181.711 -> So from here we get 6-3=3
186.778 -> Then we again local minimum because, left one is also greater and right one is also greater, so we purchased it on 1
192.336 -> But we will not sell it on 2, because 2 is not local maximum, we will well it at 4
197.805 -> Because left one from is smaller and there is nothing on right, so 4 is our local maxima
201.564 -> And 4-1=3, fine?
206.757 -> So this is basically our solution
208.498 -> That whenever we get a local minimum we will purchase it, and whenever we get local maximum we will sell it
214.259 -> And if we will keep on doing like this, then we will get our answer
217.229 -> Now, how we will find local minimum and maximum?
220.894 -> Basically, we have to iterate the whole array one time
223.689 -> And whenever you get an element that can be a local minimum then you purchase it
227.444 -> And when you have already purchased it then find local maximum
231.966 -> So in this way also you can move ahead
233.387 -> Or there is one more very simple code, when I will tell it in code form
237.631 -> So over her I have coded this, and if you will see over here
240.415 -> Then, I have not tried to find local minimum or local maximum
244.23 -> Basically, I have applied a small trick in this
247.39 -> Due to that trick I did not have to do the cumbersome code , a very simple and sweet code is ready over here
252.009 -> And the trick basically is, that suppose 5,2
255.594 -> Nothing will happen on 5, when we came at 2, then nothing will happen at 2 also
258.835 -> When we came at 6, I can see that 6 is greater then the value of yesterday's price
264.382 -> So 6>2, so I will purchase it over here
266.657 -> And I will do 6-2, so we will get 4
269.22 -> Now, I will move ahead, I will see what the current value is greater than yesterday's value? No
274.498 -> I am just checking with yesterday's value, I am not checking anything else
277.395 -> I am only checking with yesterday
278.724 -> Now I will come over here, Is this value greater than yesterday's value? Yes it is
281.62 -> So I will do 4-1=3
283.055 -> Now, I will move ahead and check i 7>4? Yes it is
288.207 -> So I will again do 7-4=3
290.847 -> So can you see over here, If I would be doing local minimum and local maximum then what would have happened
295.35 -> I would say local minimum is 1, so I will purchase it at 1,and 7 is local maxima so I will sell at 7
300.589 -> There is no need of it, I am getting 7-1=6, and by doing this 2 transaction also I am getting 6
305.687 -> I can do as much as transaction I want
306.669 -> So I am taking benefit of that over here
308.257 -> So 4-1=3, 7-4=3, and 3+3=6
313.498 -> So I am doing that only, I am checking it with the previous price, due to which my code is simple now, logic is still the same
318.187 -> Time complexity is still the same
319.873 -> We will move ahead, next is 3, is 3>7? No
322.924 -> Is 6>3? Yes
324.651 -> So we will sell at 6 which was purchased at 3, so 6-3=3
329.972 -> So we are getting 13 as our profit from here
335.497 -> I have done the same code over here, everytime I am tracking it with yesterday's price
340.679 -> So I have made a variable profit, and we will start from i=1
346.163 -> Now a[i]>a[i-1], is 2>5?No
350.716 -> Then It will not go in this condition
352.833 -> loop will come at 6
353.516 -> is 6>2? is a[i]>a[i-1]?yes 6 is greater than 2, so we will add in to profit 6-2=4, so profit will be 4
363.437 -> So now profit=4
364.853 -> Now we will move ahead, now we are on the day when price is 1
369.523 -> Is 1>6? No
371.733 -> So we will move on next day, 4>1? yes
374.861 -> So we will in profit 4-1=3, so now profit will be 4+3=7
381.795 -> Now next day, price is 7, 7>4? yes
385.202 -> Then 7-4=3, add 3 in profit
388.544 -> So 3+7=10, so profit will be 10
392.724 -> Now next day is with price 3, so 3>7? no
395.279 -> So, it will not go in this, we will come at 6, 6>3? yes
399.707 -> So we will add 6-3=3 in profit, so profit will be 10+3=13
405.49 -> So in this way without calculating local minimum and local maximum, and by applying a small trick I have simplified this code
413.529 -> So with this this problem finished over here
416.652 -> After that we will move ahead in array and solve some more problems
419.675 -> With this I'm signing off, If you liked this video then press the like button