【leetcode】122-Best Time to Buy and Sell Stock II

problem

122. Best Time to Buy and Sell Stock II

这道题由于可以无限次买入和卖出。我们都知道炒股想挣钱当然是低价买入高价抛出,那么这里我们只需要从第二天开始,如果当前价格比之前价格高,则把差值加入利润中,因为我们可以昨天买入,今日卖出,若明日价更高的话,还可以今日买入,明日再抛出。以此类推,遍历完整个数组后即可求得最大利润。

 code

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

参考

1. Leetcode_Best Time to Buy and Sell Stock II;

原文地址:https://www.cnblogs.com/happyamyhope/p/10050668.html