[leetcode] 121. Best Time to Buy and Sell Stock 解题报告

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] 121. Best Time to Buy and Sell Stock 解题报告相关的知识,希望对你有一定的参考价值。

动态规划,注意处理当前最大值小于0的情况

public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) return 0;
        int curMax = 0,result = 0;
        for (int i = 1; i < prices.length; i++) {
            curMax = Math.max(0, curMax + prices[i] - prices[i-1]);
            result = Math.max(curMax,result);
        }
        return result;
    }

 

以上是关于[leetcode] 121. Best Time to Buy and Sell Stock 解题报告的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode OJ 121. Best Time to Buy and Sell Stock

Leetcode 121 Best Time to Buy and Sell Stock

LeetCode 121: Best Time to Buy and Sell Stock

LeetCode 121 Best Time to Buy and Sell Stock

Leetcode121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock leetcode解题笔记