Welcome to the Couric interview problem.
Best time to buy Intel stock to.
So you have an array price set for which the right element is the price of a given stock on day.
Here I is, the index number now designing an algorithm to find the maximum profit.
You may complete as many transactions as you like.
That means you can buy one and sell one share of the stock multiple times.
Not that you may not engage in multiple transactions at the same time, it means you must sell the stock
before you buy again.
And this is our problem statement.
If we are given this area, we have to retain the maximum profit, if we buy at one a.m. at five, then
we have profit for if we buy at three and sell at six, then we will have profit three and poor plus
three equals to seven.
For this given area, we might have maximum profit seven.
OK, so we have to return seven for this given input.
If you're given this area's input, you have to find the maximum profit you can make.
OK, we can make as many transactions as you like.
If we sell at one, if we buy it too, then we have profit one.
If we buy at two, if we sell at three, we have profit one again.
If we buy at three, sell at four, we have profit one.
If we buy at four, if we sell at five, we have profit one.
So if we add all the profit then we get four.
So for this given input we have to return for this is pretty straightforward problem.
Let's see if we can solve it.
Let's suppose that we're giving this area first.
We're going to initialize Max.
We're pretty close to zero now.
What are we going to do?
We're going to enter through this array from this element to the end.
So our current element is one.
Now, what we're going to do, we're going to check the current element and the previous element.
If we see the current element is greater than the previous element, then we will buy at the previous
element and also at the current element.
OK, so you see one is less than seven, so we can't sell at one and buy at seven.
Then our next element five and the previous element one, we see that the previous price is less than
our current price.
So we buy at this price one and we will sell at this price five.
The profit we will have for.
So we'll add that profit to max profit, so there are plans for EchoStar for being our current element
is three and a previous element is five and the current government is less than previous.
So it will not sell at the current moment and will not buy at the previous government.
Then our next element is six, and we see that this element is greater than the previous element.
So it will sell at this element and it will buy at the previous element.
That means sell at the current price and we will buy at the previous price.
So we will have our profit six minus three goals to three.
Support plus three calls to seven the next four, and we say that four is less than the previous event,
so we're done at the end.
All right, Max Rabbit and Max were probably close to seven.
And for this input, we will return seven.
And this is how we can solve this problem.
This is pretty straightforward.
Now, let's see through the code.
First, I'm going to read a function that takes an area as input.
Let's suppose that we given this array.
Then we're going to declare a variable, max, for pretty close to zero.
Then we're going to iterate through the given area from the second element to the end.
OK, then we're going to take the previous element and the current element.
If we see the previous element is less than the current element, then what we'll do will calculate
all max profit A.C. we have one and previous seven.
So this is condition false.
Then we have five and here we will have a profit and that is five.
Minus one is four.
Then we have three and five.
And this is condition false.
Then three and six.
Here we have a profit of three, then we have six and four and the this coalition forces.
So we're done with this sort of eventual return Max profit Max probably goes to four plus three equals
to seven.
OK, so at the end, so at the end, Ulrichsen seven.
And this is my solution to this problem.
The solution will take off in time, complexity and constant space complexity here and is the length
of the given array.
All right, guys, this concept us clear.
Thanks for watching this video.
I will see you in the next video.