归档

122. 买卖股票的最佳时机II

122. 买卖股票的最佳时机II

题目

描述:

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以在 同一天 多次买卖该股票,但要确保你持有的股票不超过一股。

返回你能获得的 最大利润 。

示例 1:

  • 输入: prices = [7,1,5,3,6,4]
  • 输出: 7
  • 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。最大总利润为 4 + 3 = 7 。

示例 2:

  • 输入: prices = [1,2,3,4,5]
  • 输出: 4
  • 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。最大总利润为 4 。

示例 3:

  • 输入: prices = [7,6,4,3,1]
  • 输出: 0
  • 解释: 在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。

提示:

  • 1 <= prices.length <= 3 * 10^4
  • 0 <= prices[i] <= 10^4

题解

把交易利润拆成相邻两天的价差

本题允许多次交易,但任何时刻只能持有一股。与只能交易一次的版本不同,我们可以在一段上涨结束后卖出,等下一段上涨再买入。

例如价格为 [1,3,5],从 1 买入、到 5 卖出的利润为 4,也等于 (3 - 1) + (5 - 3)。一般地,一笔交易的利润等于持有期间所有相邻价差的总和。

一笔交易的利润等于它持有期间的相邻价差之和,其中负价差只会拉低利润,所以它不超过这段时间里的正价差之和。各笔交易的持有期间互不重叠,加起来就得到:任何交易方案的收益都不会超过全部正价差之和。这个上界也能实现:把每个连续上涨段作为一笔交易,段内一直持有,下降前卖出。各笔交易互不重叠,始终满足最多持有一股的限制。于是答案就是所有正价差之和。

对 [7,1,5,3,6,4],只有 1 → 5 和 3 → 6 两段上涨,贡献 4 + 3 = 7。代码从第二天开始比较,只在当天价格高于前一天时累加差值,不需要记录实际的买卖日期。

1
2
3
4
5
6
7
8
9
10
11
public int maxProfit(int[] prices) {
    int totalProfit = 0;

    for (int i = 1; i < prices.length; i++) {
        if (prices[i] > prices[i - 1]) {
            totalProfit += prices[i] - prices[i - 1];
        }
    }

    return totalProfit;
}

时间复杂度是 $O(n)$,因为整个数组只遍历了一遍;空间复杂度是 $O(1)$,因为只用了一个额外变量。

只有一天或价格始终不涨时,没有正价差,结果为 0。这里可以直接累加正价差,依赖于题目允许多次交易且没有手续费、冷冻期等额外限制。

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Arrays;

public class Main {

    public static int maxProfit(int[] prices) {
        int totalProfit = 0;

        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                totalProfit += prices[i] - prices[i - 1];
            }
        }

        return totalProfit;
    }

    public static void main(String[] args) {
        int[] prices1 = new int[]{7, 1, 5, 3, 6, 4};

        System.out.println("示例 1 输入:");
        System.out.println("prices = " + Arrays.toString(prices1));

        int result1 = maxProfit(prices1);

        System.out.println("示例 1 输出: " + result1);

        int[] prices2 = new int[]{1, 2, 3, 4, 5};

        System.out.println("示例 2 输入:");
        System.out.println("prices = " + Arrays.toString(prices2));

        int result2 = maxProfit(prices2);

        System.out.println("示例 2 输出: " + result2);

        int[] prices3 = new int[]{7, 6, 4, 3, 1};

        System.out.println("示例 3 输入:");
        System.out.println("prices = " + Arrays.toString(prices3));

        int result3 = maxProfit(prices3);

        System.out.println("示例 3 输出: " + result3);
    }
}

运行结果(OpenJDK 21.0.12):

1
2
3
4
5
6
7
8
9
示例 1 输入:
prices = [7, 1, 5, 3, 6, 4]
示例 1 输出: 7
示例 2 输入:
prices = [1, 2, 3, 4, 5]
示例 2 输出: 4
示例 3 输入:
prices = [7, 6, 4, 3, 1]
示例 3 输出: 0
本文由作者按照 CC BY 4.0 进行授权