Given an array, which i-th element is a given stock i-day prices.
Design an algorithm to compute the maximum profit you can get. You can close more deals (buy and sell a stock) as much as possible.
Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).
Example 1: Input: [7,1,5,3,6,4] Output: 7
Explanation: Day 2 (stock price = 1) when buying on day 3 (stock price = 5), when sold, this transaction can profit = 5-1 = 4.
Then, on day 4 (stock price = 3) When buying on day 5 (stock price = 6), when sold, this transaction can profit = 6-3 = 3.
Then, on day 4 (stock price = 3) When buying on day 5 (stock price = 6), when sold, this transaction can profit = 6-3 = 3.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0; int Max =0;
for(int i=1;i<prices.size();i++)
{
profit = prices[i]-prices[i-1];
if(profit >0)
Max = Max+profit;
}
return Max;
}
};