```
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length==0) return 0;
int min = prices[0];
int maxProfit = 0;
int currMaxProfit = 0;
for(int i=1;i<prices.length;i++) {
//if the current price is greater than min and also greater than previous day's price,
//get the max profit since this is an increasing sequence of prices.
if(prices[i] > min && prices[i] > prices[i-1]) {
currMaxProfit = Math.max(currMaxProfit, prices[i]-min);
}
//otherwise, the min price will be the latest price and the profit earned so far is added to the total profit.
//currProfit is set to 0 and will be calculated for the next increasing sequence.
else{
min = prices[i];
maxProfit += currMaxProfit;
currMaxProfit = 0;
}
}
return maxProfit+currMaxProfit;
}
}
```