友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

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 * 104

  • 0 <= prices[i] <= 104

思路分析

0122 01
0122 02
0122 03
0122 04
0122 05
0122 06
0122 07
0122 08

动态规划的思路

0122 19

在第 i 天,有“满仓” \(dfs(i, 1)\) 和“空仓” \(dfs(i, 0)\) 两个状态:

  1. 如果是满仓,有两种可能:

    1. i-1 天“继承”过来: \(dfs(i-1, 1)\)

    2. i-1 天“空仓”,在当天买入: \(dfs(i-1, 0) - prices[i]\)

  2. 如果是空仓,有两种可能:

    1. i-1 天“继承”过来: \(dfs(i-1, 0)\)

    2. i-1 天“空仓”,在当天买入: \(dfs(i-1, 1) + prices[i]\)

综上,得出:

  1. “满仓” : \(dfs(i, 1) = math(dfs(i-1, 1), dfs(i-1, 0) - prices[i])\)

  2. “空仓”: \(dfs(i, 0) = math(dfs(i-1, 0), dfs(i-1, 1) + prices[i])\)

那么,“满仓” \(dfs(i, 1)\) 和“空仓” \(dfs(i, 0)\),哪个符合条件呢?正常情况下,肯定是“空仓” \(dfs(i, 0)\) 的利润最高。

还有一个问题:起点在哪里?如果是“满仓”,有可能前一个股票价格是无穷大,所以,取 Integer.MIN_VALUE;如果是“空仓”,没有买卖,那么利润就是 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * Runtime: 1 ms, faster than 87.57% of Java online submissions for Best Time to Buy and Sell Stock II.
 * Memory Usage: 42.8 MB, less than 5.71% of Java online submissions for Best Time to Buy and Sell Stock II.
 *
 * Copy from: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-lab/[一个方法团灭 6 道股票问题 - 最佳买卖股票时机含冷冻期 - 力扣(LeetCode)]
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-04 10:55
 */
public int maxProfitDp(int[] prices) {
    int dp0 = 0;
    int dp1 = Integer.MIN_VALUE;
    for (int i = 0; i < prices.length; i++) {
        dp0 = Math.max(dp0, dp1 + prices[i]);
        dp1 = Math.max(dp1, dp0 - prices[i]);
    }
    return dp0;
}
/**
 * Runtime: 1 ms, faster than 85.09% of Java online submissions for Best Time to Buy and Sell Stock II.
 *
 * Memory Usage: 37 MB, less than 100.00% of Java online submissions for Best Time to Buy and Sell Stock II.
 */
public int maxProfit(int[] prices) {
    if (Objects.isNull(prices) || prices.length == 0) {
        return 0;
    }
    int result = 0;
    for (int i = 1; i < prices.length; i++) {
        if (prices[i - 1] < prices[i]) {
            result += prices[i] - prices[i - 1];
        }
    }
    return result;
}
/**
 * Runtime: 1 ms, faster than 85.09% of Java online submissions for Best Time to Buy and Sell Stock II.
 *
 * Memory Usage: 37 MB, less than 100.00% of Java online submissions for Best Time to Buy and Sell Stock II.
 *
 * 我的思路,别人的代码。
 */
public int maxProfitPeakValleyApproach(int[] prices) {
    if (Objects.isNull(prices) || prices.length == 0) {
        return 0;
    }
    int i = 0;
    int valley = prices[0];
    int peak = prices[0];
    int result = 0;
    while (i < prices.length - 1) {
        while (i < prices.length - 1 && prices[i] > prices[i + 1]) {
            i++;
        }
        valley = prices[i];
        while (i < prices.length - 1 && prices[i] < prices[i++]) {
            i++;
        }
        peak = prices[i];
        result += peak - valley;
    }

    return result;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-09-14 14:48:19
 */
public int maxProfit(int[] prices) {
  int result = 0;
  for (int i = 1; i < prices.length - 1; i++) {
    // 只要发现价差就进行交易
    if (prices[i - 1] < prices[i]) {
      result += (prices[i] - prices[i - 1]);
    }
  }
  return result;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2025-04-27 07:59:27
 */
public int maxProfit(int[] prices) {
  int result = 0;
  for (int i = 1; i < prices.length; i++) {
    if (prices[i - 1] < prices[i]) {
      // 见好就收
      result += prices[i] - prices[i - 1];
    }
  }
  return result;
}
 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
/**
 * 暴力破解(198/202)
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2026-01-17 15:56:56
 */
public int maxProfit(int[] prices) {
  return dfs(prices, prices.length - 1, false);
}

private int dfs(int[] prices, int index, boolean hold) {
  if (index < 0) {
    if (hold) {
      return Integer.MIN_VALUE;
    } else {
      return 0;
    }
  }
  if (hold) {
    return Math.max(dfs(prices, index - 1, true),
      dfs(prices, index - 1, false) - prices[index]);
  }
  return Math.max(dfs(prices, index - 1, false),
    dfs(prices, index - 1, true) + prices[index]);
}
 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
/**
 * 暴力破解(198/202)-> 备忘录(5.16%)
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2026-01-17 16:13:38
 */
public int maxProfit(int[] prices) {
  int[][] memo = new int[2][prices.length];
  for (int[] ints : memo) {
    Arrays.fill(ints, Integer.MIN_VALUE);
  }
  return dfs(prices, prices.length - 1, 0, memo);
}

private int dfs(int[] prices, int index, int hold, int[][] memo) {
  if (index < 0) {
    if (hold == 1) {
      return Integer.MIN_VALUE;
    } else {
      return 0;
    }
  }
  if (memo[hold][index] != Integer.MIN_VALUE) {
    return memo[hold][index];
  }
  if (hold == 1) {
    return memo[hold][index] = Math.max(dfs(prices, index - 1, 1, memo),
      dfs(prices, index - 1, 0, memo) - prices[index]);
  }
  return memo[hold][index] = Math.max(dfs(prices, index - 1, 0, memo),
    dfs(prices, index - 1, 1, memo) + prices[index]);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
/**
 * 暴力破解(198/202)-> 备忘录(5.16%)-> 动态规划(43.37%)
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2026-01-17 16:20:14
 */
public int maxProfit(int[] prices) {
  int[][] dp = new int[2][prices.length + 1];
  Arrays.fill(dp[0], 0);
  Arrays.fill(dp[1], Integer.MIN_VALUE);
  for (int i = 1; i <= prices.length; i++) {
    // dp[0][index] = max(dp[0][index-1], dp[1][index] + prices[index])
    // dp[1][index] = max(dp[0][index-1], dp[0][index] - prices[index])
    dp[0][i] = Math.max(dp[0][i - 1], dp[1][i - 1] + prices[i - 1]);
    dp[1][i] = Math.max(dp[1][i - 1], dp[0][i - 1] - prices[i - 1]);
  }
  return dp[0][prices.length];
}