1 Greedy algorithm
class Solution {
public:
int maxProfit(vector<int>& prices) {
int curDiff = 0;
int money = 0;
for (int i = 0; i < prices.size(); i++) {
if (i == 0) continue;
curDiff = prices[i] - prices[i-1];
if (curDiff > 0)
money += curDiff;
}
return money;
}
};
Thanks
The picture comes from the official account of "Code Random Record", welcome everyone to pay attention to the official account of this big guy