class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0, curPrice;
bool bought = false;
for(int i=0; i<prices.size(); i++) {
if(i == prices.size()-1) {
if(bought && prices[i] > curPrice)
profit += prices[i]-curPrice;
break;
}
if( prices[i] > prices[i+1] && bought) {
profit += prices[i]-curPrice;
bought = false;
}
else if(prices[i] < prices[i+1] && !bought) {
bought = true;
curPrice = prices[i];
}
}
return profit;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.empty()) return 0;
int maxprofit = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
}
};