美文网首页
LeetCode-买股票的最佳时机 II

LeetCode-买股票的最佳时机 II

作者: 沙漠小舟 | 来源:发表于2020-04-01 21:44 被阅读0次

题目链接 => 戳这里

题目描述

解析

其实这道题可以考虑成将所有的点描绘到一张图中,然后将所有上升段的差值相加即可;

解法

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

相关文章

网友评论

      本文标题:LeetCode-买股票的最佳时机 II

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