Post

Best Time to Buy and Sell Stock

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2) {
            return 0;
        }

        int min = prices[0];
        // max profit
        int max = 0;

        for (int i = 0; i < prices.length; ++i) {
            if (prices[i] < min) {
                min = prices[i];
            }
            // current profit
            int curr = prices[i] - min;
            if (curr > max) {
                max = curr;
            }
        }

        return max;
    }
}

Complexity

  • Time = O(n)
  • Space = O(1)

股票的买入价格和卖出价格两个数字组成一个数对,那么利润就是这个数对的差值。

最大利润就是数组中所有数对的最大差值。

This post is licensed under CC BY 4.0 by the author.