lc 买卖股票的最佳时机 II

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

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() == 0) return 0;
        if(prices.size() == 0) return 0;
        int ans = 0;
        for(int i = 1; i < prices.size(); i++) {
            if(prices[i] - prices[i-1] > 0) ans += (prices[i] - prices[i-1]);
        }
        return ans;
    }
};
View Code

思路:和原题完全不同,若不限次数取则贪心即可,贪心依据:无限次数,取增量>0即可,若增量一直>0,中点卖出买入和中点不作为无区别。

posted on 2020-05-18 00:38  FriskyPuppy  阅读(156)  评论(0编辑  收藏  举报

导航