Leetcode|Medium|Stock|122. The Best Time to Buy and Sell Stocks II

Insert picture description here

1 Greedy algorithm

Insert picture description here

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;
    }
};

Insert picture description here

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

Guess you like

Origin blog.csdn.net/SL_World/article/details/114881115