122. Best Time to Buy and Sell Stock II

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

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