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

122. Best Time to Buy and Sell S

作者: DrunkPian0 | 来源:发表于2017-04-08 23:00 被阅读11次

    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 S

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