Skip to content

Commit b9aaa11

Browse files
committedSep 14, 2024·
二刷121
1 parent 9ca9af7 commit b9aaa11

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed
 

Diff for: ‎docs/0121-best-time-to-buy-and-sell-stock.adoc

+23-29
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,37 @@ Output: 0
2424
Explanation: In this case, no transaction is done, i.e. max profit = 0.
2525
----
2626

27-
针对 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)^] 这个解题框架,进行小试牛刀。
27+
== 思路分析
2828

29-
== 参考资料
30-
31-
. 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)^]
32-
. https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/dong-tai-gui-hua-shen-ru-fen-xi-by-wang-yan-19/[动态规划深入分析 - 最佳买卖股票时机含冷冻期 - 力扣(LeetCode)^]
29+
一次遍历,在遍历中找最大值减去最小值即可。
3330

34-
Say you have an array for which the _i_^th^ element is the price of a given stock on day _i_.
31+
image::images/0121-01.gif[{image_attr}]
3532

36-
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
37-
38-
Note that you cannot sell a stock before you buy one.
39-
40-
*Example 1:*
41-
42-
[subs="verbatim,quotes,macros"]
33+
[[src-0121]]
34+
[tabs]
35+
====
36+
一刷::
37+
+
38+
--
39+
[{java_src_attr}]
4340
----
44-
*Input:* [7,1,5,3,6,4]
45-
*Output:* 5
46-
*Explanation:* Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
47-
Not 7-1 = 6, as selling price needs to be larger than buying price.
41+
include::{sourcedir}/_0121_BestTimeToBuyAndSellStock.java[tag=answer]
4842
----
43+
--
4944
50-
*Example 2:*
51-
52-
[subs="verbatim,quotes,macros"]
45+
二刷::
46+
+
47+
--
48+
[{java_src_attr}]
5349
----
54-
*Input:* [7,6,4,3,1]
55-
*Output:* 0
56-
*Explanation:* In this case, no transaction is done, i.e. max profit = 0.
50+
include::{sourcedir}/_0121_BestTimeToBuyAndSellStock_2.java[tag=answer]
5751
----
52+
--
53+
====
5854

5955

56+
== 参考资料
6057

61-
[[src-0121]]
62-
[{java_src_attr}]
63-
----
64-
include::{sourcedir}/_0121_BestTimeToBuyAndSellStock.java[tag=answer]
65-
----
66-
58+
. 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 道股票问题^]
59+
. https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/dong-tai-gui-hua-shen-ru-fen-xi-by-wang-yan-19/[动态规划深入分析 - 最佳买卖股票时机含冷冻期 - 力扣(LeetCode)^]
60+
. https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/solutions/1692872/by-jyd-cu90/[121. 买卖股票的最佳时机 - 动态规划,清晰图解^]

Diff for: ‎docs/images/0121-01.gif

12.7 MB
Loading

Diff for: ‎logbook/202406.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@
570570
|{doc_base_url}/0236-lowest-common-ancestor-of-a-binary-tree.adoc[题解]
571571
|想清楚怎么判断是否为公共祖先。
572572

573+
|{counter:codes}
574+
|{leetcode_base_url}/best-time-to-buy-and-sell-stock/[121. Best Time to Buy and Sell Stock^]
575+
|{doc_base_url}/0121-best-time-to-buy-and-sell-stock.adoc[题解]
576+
|
577+
573578
|===
574579

575580
截止目前,本轮练习一共完成 {codes} 道题。

Diff for: ‎src/main/java/com/diguage/algo/leetcode/_0121_BestTimeToBuyAndSellStock.java

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class _0121_BestTimeToBuyAndSellStock {
4040
* Memory Usage: 42.6 MB, less than 5.31% of Java online submissions for Best Time to Buy and Sell Stock.
4141
*
4242
* 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)]
43+
*
44+
* @author D瓜哥 · https://www.diguage.com
45+
* @since 2020-01-01 13:01
4346
*/
4447
public int maxProfitDp(int[] prices) {
4548
int dp0 = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0121_BestTimeToBuyAndSellStock_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-15 07:44:20
8+
*/
9+
public int maxProfit(int[] prices) {
10+
int result = 0;
11+
int min = 100000;
12+
for (int p : prices) {
13+
min = Math.min(min, p);
14+
result = Math.max(result, p - min);
15+
}
16+
return result;
17+
}
18+
// end::answer[]
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.