From 7cef253757777c2dbb92a7cb164357c4303c022c Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Tue, 23 Nov 2021 18:33:52 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86121.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E7=9A=84java=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 64 ++++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 3aff6b89..89f0637a 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -195,23 +195,26 @@ public: ## 其他语言版本 Java: + +> 贪心法: + ```java -// 贪心思路 class Solution { public int maxProfit(int[] prices) { - int minprice = Integer.MAX_VALUE; - int maxprofit = 0; - for (int i = 0; i < prices.length; i++) { - if (prices[i] < minprice) { - minprice = prices[i]; - } else if (prices[i] - minprice > maxprofit) { - maxprofit = prices[i] - minprice; - } + // 找到一个最小的购入点 + int low = Integer.MAX_VALUE; + // res不断更新,直到数组循环完毕 + int res = 0; + for(int i = 0; i < prices.length; i++){ + low = Math.min(prices[i], low); + res = Math.max(prices[i] - low, res); } - return maxprofit; + return res; } } ``` +> 动态规划:版本一 + ```java // 解法1 class Solution { @@ -233,30 +236,27 @@ class Solution { } ``` +> 动态规划:版本二 + ``` java -class Solution { // 动态规划解法 - public int maxProfit(int[] prices) { - // 可交易次数 - int k = 1; - // [天数][交易次数][是否持有股票] - int[][][] dp = new int[prices.length][k + 1][2]; - - // bad case - dp[0][0][0] = 0; - dp[0][0][1] = Integer.MIN_VALUE; - dp[0][1][0] = Integer.MIN_VALUE; - dp[0][1][1] = -prices[0]; - - for (int i = 1; i < prices.length; i++) { - for (int j = k; j >= 1; j--) { - // dp公式 - dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]); - dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]); - } - } - - return dp[prices.length - 1][k][0] > 0 ? dp[prices.length - 1][k][0] : 0; +class Solution { + public int maxProfit(int[] prices) { + int[] dp = new int[2]; + dp[0] = -prices[0]; + dp[1] = 0; + // 可以参考斐波那契问题的优化方式 + // dp[0] 和 dp[1], 其实是第 0 天的数据 + // 所以我们从 i=1 开始遍历数组,一共有 prices.length 天, + // 所以是 i<=prices.length + for (int i = 1; i <= prices.length; i++) { + int temp = dp[0]; + // 前一天持有;或当天买入 + dp[0] = Math.max(temp, -prices[i - 1]); + // 前一天卖出;或当天卖出, 当天要卖出,得前一天持有才行 + dp[1] = Math.max(dp[1], temp + prices[i - 1]); } + return dp[1]; + } } ``` From 71f1410d8d0d9d71d9208b6a6ee098c70b3d3303 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 09:25:23 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86121=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=E7=89=88=E6=9C=AC=E4=BA=8C=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E9=A2=98=E4=BB=A3=E7=A0=81^C=E9=A2=98=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 89f0637a..45b61666 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -249,11 +249,14 @@ class Solution { // 所以我们从 i=1 开始遍历数组,一共有 prices.length 天, // 所以是 i<=prices.length for (int i = 1; i <= prices.length; i++) { - int temp = dp[0]; // 前一天持有;或当天买入 - dp[0] = Math.max(temp, -prices[i - 1]); + dp[0] = Math.max(dp[0], -prices[i - 1]); + // 如果 dp[0] 被更新,那么 dp[1] 肯定会被更新为正数的 dp[1] + // 而不是 dp[0]+prices[i-1]==0 的0, + // 所以这里使用会改变的dp[0]也是可以的 + // 当然 dp[1] 初始值为 0 ,被更新成 0 也没影响 // 前一天卖出;或当天卖出, 当天要卖出,得前一天持有才行 - dp[1] = Math.max(dp[1], temp + prices[i - 1]); + dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]); } return dp[1]; } From eacf15d7a3a8042eb8c0181c9d7fb8a438f39e17 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 09:31:54 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86122=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=A9=BA=E9=97=B4=E7=9A=84java=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0122.买卖股票的最佳时机II.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index d4575bd6..5b117563 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -167,6 +167,25 @@ class Solution { // 动态规划 } ``` +```java +// 优化空间 +class Solution { + public int maxProfit(int[] prices) { + int[] dp=new int[2]; + // 0表示持有,1表示卖出 + dp[0]=-prices[0]; + dp[1]=0; + for(int i=1; i<=prices.length; i++){ + // 前一天持有; 或当天卖出然后买入 + dp[0]=Math.max(dp[0], dp[1]-prices[i-1]); + // 前一天卖出; 或当天卖出,当天卖出,得先持有 + dp[1]=Math.max(dp[1], dp[0]+prices[i-1]); + } + return dp[1]; + } +} +``` + ### Python From 5fc35368c1feba9a87cea44d603ebb8b646e8654 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 09:37:18 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BA=86123=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=A9=BA=E9=97=B4=E5=86=99=E6=B3=95=E7=9A=84java?= =?UTF-8?q?=E8=A7=A3=E9=A2=98=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0123.买卖股票的最佳时机III.md | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 9c269978..db3f0278 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -188,7 +188,7 @@ dp[1] = max(dp[1], dp[0] - prices[i]); 如果dp[1]取dp[1],即保持买入股 ## 其他语言版本 -Java: +### Java ```java // 版本一 @@ -221,25 +221,30 @@ class Solution { // 版本二: 空间优化 class Solution { public int maxProfit(int[] prices) { - int len = prices.length; - int[] dp = new int[5]; - dp[1] = -prices[0]; - dp[3] = -prices[0]; - - for (int i = 1; i < len; i++) { - dp[1] = Math.max(dp[1], dp[0] - prices[i]); - dp[2] = Math.max(dp[2], dp[1] + prices[i]); - dp[3] = Math.max(dp[3], dp[2] - prices[i]); - dp[4] = Math.max(dp[4], dp[3] + prices[i]); + int[] dp=new int[4]; + // 存储两天的状态就行了 + // dp[0]代表第一次买入 + dp[0]=-prices[0]; + // dp[1]代表第一次卖出 + dp[1]=0; + // dp[2]代表第二次买入 + dp[2]=-prices[0]; + // dp[3]代表第二次卖出 + dp[3]=0; + for(int i=1; i<=prices.length; i++){ + // 要么保持不变,要么没有就买,有了就卖 + dp[0]=Math.max(dp[0], -prices[i-1]); + dp[1]=Math.max(dp[1], dp[0]+prices[i-1]); + // 这已经是第二天了,所以得加上前一天卖出去的价格 + dp[2]=Math.max(dp[2], dp[1]-prices[i-1]); + dp[3]=Math.max(dp[3], dp[2]+prices[i-1]); } - - return dp[4]; + return dp[3]; } } ``` - -Python: +### Python > 版本一: ```python @@ -308,7 +313,7 @@ func max(a,b int)int{ -JavaScript: +### JavaScript > 版本一: @@ -347,7 +352,7 @@ const maxProfit = prices => { }; ``` -Go: +### Go > 版本一: ```go @@ -381,5 +386,7 @@ func max(a, b int) int { ``` + + -----------------------