2-209-(LeetCode-121) 买卖股票的最佳时机

发布时间 2023-04-17 16:44:20作者: 白露~

 

1. 题目

 

https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/

121. 买卖股票的最佳时机

 

2. 解法

2.1 解法一:动态规划 

 

2.2 解法二:非动态规划 

        if (prices.length < 2) {
            return 0;
        }

        int min = prices[0];
        int max = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] > min) {
                max = Math.max(max, prices[i] - min);
            } else {
                min = prices[i];
            }
        }
        return max;

  

3. 总结