121. Best Time to Buy and Sell Stock

最大子段和问题的变形:
此题相关的算法是:Kadane算法
代码:

public:
    int maxProfit(vector& prices) {
        int n = prices.size();
        int sum =0;
        int maxn = 0;
       
        for(int i=1; i0){
                maxn = maxn>sum ? maxn : sum;
            }else{
                sum=0;  //没有必要继续下去
            }
        }   
        return maxn;
    }

你可能感兴趣的:(121. Best Time to Buy and Sell Stock)