友情支持

如果您觉得这个笔记对您有所帮助,看在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
  • 一刷

  • 二刷

  • 三刷

 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;
}