美文网首页
122. Best Time to Buy and Sell S

122. Best Time to Buy and Sell S

作者: larrymusk | 来源:发表于2017-11-24 21:42 被阅读0次

    这题可以不限次数交易,因此前后比较只要>0就累加入总收入里面

    int maxProfit(int* prices, int pricesSize) {
        int sum = 0;
        int i = 0;
        for(i = 1; i < pricesSize; i++){
            if(prices[i]-prices[i-1] > 0)
                sum += prices[i]-prices[i-1];
        }
        
        return sum;
    }

    相关文章

      网友评论

          本文标题:122. Best Time to Buy and Sell S

          本文链接:https://www.haomeiwen.com/subject/anxqbxtx.html