class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) {
return 0;
}
int profit = 0;
int diff = 0;
for(int i = 0; i < prices.size() - 1; ++i) {
if(prices[i + 1] >= prices[i]) {
diff += prices[i + 1] - prices[i];
} else {
profit += diff;
diff = 0;
}
}
profit += diff;
return profit;
}
};
No comments:
Post a Comment