Best time to buy and sell stock II

easy 原题链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii

Best time to buy and sell stock II

原题链接

描述

例子

解法 greedy

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int maxprofit=0,hold=-prices[0];
        for(int i=1; i<prices.size(); i++){
            hold=max(hold, maxprofit-prices[i]);
            maxprofit=max(maxprofit, hold+prices[i]);
        }
        return maxprofit;
    }
};

最后更新于

这有帮助吗?