 What's up guys, mine is Michael and welcome to my YouTube channel today. We're gonna do another Lee code Challenge question. I've really been not really doing these questions at all for the days But this is the best time to buy and sell stock All right So let's say you have an array where the ith element represents the price of a stock on the day I so we have to design an algorithm to find the maximum profit you may complete as many Transaction you like buy and sell one of their share stocks multiple times You may not engage in multiple transactions at the same time. You must sell the stock before you buy again so in this case we have the 7 1 5 3 6 4 array and the output is 7 and the reason why is because if you were to buy 1 like buy on day 2 and then sell on day 3, which day 3 is they're counting Index 1 so 1 2 3 day 3 is 5 If you buy 1 and then sell day 5 when the price is 5 You make a profit of 5 minus 1 is equal to 4 then if you buy on day 4 which is The price of 3 and then sell on day 5 price of 6 You're gonna make a profit of 6 minus 3 equal to 3 okay, and Let's look at the test cases the second test case If you were to buy on day 1 the price is 1 and so on day 5 you would make a profit of 5 5 minus 1 is 4. Yeah, so if you were by on day 1 you were to sell on day 5 When the price is 5 5 You would have a profit of 5 minus 4 and just go for it. Okay, so Essentially, they want you to find the They really just want you to find the difference Right sum up the differences, but It's not just like find the next difference that's right next to me because like Let's say I have 7 and then I subtract 1 that'll be minus 6 so I lose money Right like if I do like 1 minus 7 that'll be minus 6. So that's not really what I want Let's look at this example 3 7 6 4 3 1 the output is 0 because no transaction done maximum profit is 0 Okay, so if you're by on 7 all the values afterwards are less than it so You so you can't really sell anything with a better price So your maximum profit is 0. So I actually solved this problem and Here's what I did This is really ugly code but What I did was I started from the beginning and I went to the end and then I pretty much just kept track of the index of Whichever value is greater than it Right. So like if I have 7 then I find a value. That's like larger than 7 then I'm going to subtract I'm gonna stop right. I'm gonna stop and I'm gonna Add my profit. I take I subtract the large value from the small value and then yeah, then I Re-like reset my new index at where that starting point ended it from so that's what I did and It passed actually actually passed the test cases, but I'm not really sure like It's not really it's not really a good solution. I guess So what I did was like I went through it and then I pretty much found whichever price I was larger than it in the current one. I'm at right. So this is this. This is just going through every single price afterwards Comparing it with left and right the one next to it and finding if I get to a larger price Then I stop and I subtract by the difference at each indexes That's why I did and I'll add them my maximum profit then I Re-ad my whichever index I stopped at I set that to equal the new index and I returned max profit and Ideally worst-case scenario is that you have to Maybe this is O of N. I don't know. I'm not really sure worst-case might be over and Squared When I don't know no Take it is still open. I Don't know I'm not really sure. I don't know the time complexity to this. I should really learn more about that, but um Let's look at the actual solution So if you look at the actual solution what they did was They wow they actually graphed every single profit. Okay, so they graphed every single profit Like every day with its profit. So day one they have profit one day two. They have seven day three. They have two day four they have Four they have day four they have three day five they have six they six they have seven Okay, so they graphed every single of this and then from the above graph you see that the sum of the differences a b and c from every single ones that are greater than it is really just the difference between the heights of the Smallest and the largest peak right smallest and the largest peak. So What did they do? They just looped through and Wow, that's all they did Okay, well, okay, maybe we should use this code instead. So they loop through and then if they found a I was just taking like what I did sort of but if they found a price That's larger than the previous price then they add the differences of The current price and the previous price Hmm Okay, wow, that's way shorter code. Yeah, that's that's pretty much the solution So you're gonna loop through from every single price and then if you find a Price that's larger than the previous price You're just gonna add the difference of the largest price and the previous the larger price and the previous price And then after that you just return it. So yeah one single pass over and over one constant space Yeah, so that's how you do this question It's basically just trying to find you the differences when you mean like Selling for profit in the price. Yeah, that's how you do this question raycom subscribe. I hope you guys enjoy this video It's pretty short. Yeah, peace