Skip to content

Commit 76f8917

Browse files
committed
三刷122
1 parent 22d2bbb commit 76f8917

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

docs/0122-best-time-to-buy-and-sell-stock-ii.adoc

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
1+
12
[#0122-best-time-to-buy-and-sell-stock-ii]
2-
= 122. Best Time to Buy and Sell Stock II
3+
= 122. 买卖股票的最佳时机 II
34

4-
{leetcode}/problems/best-time-to-buy-and-sell-stock-ii/[LeetCode - Best Time to Buy and Sell Stock II^]
5+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/[LeetCode - 122. 买卖股票的最佳时机 II ^]
56

6-
Say you have an array for which the _i^th^_ element is the price of a given stock on day _i_.
7+
给你一个整数数组 `prices` ,其中 `prices[i]` 表示某支股票第 `i` 天的价格。
78

8-
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
9+
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 *最多* 只能持有 *一股* 股票。你也可以先购买,然后在 *同一天* 出售。
910

10-
*Note:* You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
11+
返回 _你能获得的 *最大* 利润_
1112

12-
.Example 1:
13-
----
14-
Input: [7,1,5,3,6,4]
15-
Output: 7
16-
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
17-
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
18-
----
13+
*示例 1:*
14+
15+
....
16+
输入:prices = [7,1,5,3,6,4]
17+
输出:7
18+
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。
19+
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3。
20+
最大总利润为 4 + 3 = 7 。
21+
....
22+
23+
*示例 2:*
24+
25+
....
26+
输入:prices = [1,2,3,4,5]
27+
输出:4
28+
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。
29+
最大总利润为 4 。
30+
....
31+
32+
*示例 3:*
33+
34+
....
35+
输入:prices = [7,6,4,3,1]
36+
输出:0
37+
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0。
38+
....
39+
40+
41+
*提示:*
42+
43+
* `1 \<= prices.length \<= 3 * 10^4^`
44+
* `0 \<= prices[i] \<= 10^4^`
1945
20-
.Example 2:
21-
----
22-
Input: [1,2,3,4,5]
23-
Output: 4
24-
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
25-
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
26-
engaging multiple transactions at the same time. You must sell before buying again.
27-
----
2846
29-
.Example 3:
30-
----
31-
Input: [7,6,4,3,1]
32-
Output: 0
33-
Explanation: In this case, no transaction is done, i.e. max profit = 0.
34-
----
3547
3648
== 思路分析
3749

@@ -74,8 +86,18 @@ include::{sourcedir}/_0122_BestTimeToBuyAndSellStockII.java[tag=answer]
7486
include::{sourcedir}/_0122_BestTimeToBuyAndSellStockII_2.java[tag=answer]
7587
----
7688
--
89+
90+
三刷::
91+
+
92+
--
93+
[{java_src_attr}]
94+
----
95+
include::{sourcedir}/_0122_BestTimeToBuyAndSellStockIi_3.java[tag=answer]
96+
----
97+
--
7798
====
7899

100+
79101
== 参考资料
80102

81103
. https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/solutions/8610/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-lab/[309. 买卖股票的最佳时机含冷冻期 - 一个方法团灭 6 道股票问题^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,11 @@ endif::[]
518518
|{doc_base_url}/0069-sqrtx.adoc[题解]
519519
|✅ 二分查找。使用一个单独变量来保存最后一个小于等于的中间值,那么即可直接获取答案。
520520

521+
|{counter:codes2503}
522+
|{leetcode_base_url}/best-time-to-buy-and-sell-stock-ii/[122. 买卖股票的最佳时机 II^]
523+
|{doc_base_url}/0122-best-time-to-buy-and-sell-stock-ii.adoc[题解]
524+
|✅ 有差价就买卖,见好就收。
525+
521526
|===
522527

523528
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0122_BestTimeToBuyAndSellStockIi_3 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-27 07:59:27
8+
*/
9+
public int maxProfit(int[] prices) {
10+
int result = 0;
11+
for (int i = 1; i < prices.length; i++) {
12+
if (prices[i - 1] < prices[i]) {
13+
// 见好就收
14+
result += prices[i] - prices[i - 1];
15+
}
16+
}
17+
return result;
18+
}
19+
// end::answer[]
20+
}

0 commit comments

Comments
 (0)