DP

122. Best Time to Buy and Sell S

2017-04-08  本文已影响11人  DrunkPian0

You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times).

这题可以买任意多次交易。
有点上帝视角的意思。

    public int maxProfit(int[] prices) {
        if (prices.length < 2) return 0;
        int total = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] - prices[i - 1] > 0) {
                total += prices[i] - prices[i - 1];
            }
        }
        return total;
    }

ref:
http://blog.csdn.net/linhuanmars/article/details/23164149

上一篇 下一篇

猜你喜欢

热点阅读