From 30886018c4fc1174914a7db707c15879fc9c6392 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:12:39 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86309=E5=92=8C714?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E9=97=AE=E9=A2=98=E4=B8=80=E7=BB=B4=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=BC=98=E5=8C=96=E7=A9=BA=E9=97=B4=E7=9A=84java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...09.最佳买卖股票时机含冷冻期.md | 23 +++++++++++++++++++ ...买卖股票的最佳时机含手续费.md | 18 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 8520e655..8c6ab288 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -185,6 +185,29 @@ class Solution { } ``` +```java +// 一维数组优化 +class Solution { + public int maxProfit(int[] prices) { + // dp[2]和dp[3]用来存储冷冻期的数据 + int[] dp=new int[4]; + // 0表示持有,1表示卖出 + dp[0] = -prices[0]; + dp[1] = 0; + for(int i = 1; i <= prices.length; i++){ + // 使用临时变量来保存dp[0], dp[2] + // 因为马上dp[0]和dp[2]的数据都会变 + int temp = dp[0]; + int temp1 = dp[2]; + dp[0] = Math.max(dp[0], Math.max(dp[3], dp[1]) - prices[i-1]); + dp[1] = Math.max(dp[1], dp[3]); + dp[2] = temp + prices[i-1]; + dp[3] = temp1; + } + return Math.max(dp[3],Math.max(dp[1],dp[2])); + } +} +``` Python: diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index afa2e2f6..964d5ea9 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -195,8 +195,26 @@ class Solution { // 动态规划 } ``` +```java +// 一维数组优化 +class Solution { + public int maxProfit(int[] prices, int fee) { + int[] dp = new int[2]; + 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] - fee); + } + return dp[1]; + } +} +``` + + Python: + ```python class Solution: # 贪心思路 def maxProfit(self, prices: List[int], fee: int) -> int: From 95a8752dd5d3c522b16b237a37df09e03379b35e Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:19:34 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86121=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84=E4=BC=98=E5=8C=96=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E7=9A=84java=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 45b61666..2815cd47 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -194,7 +194,7 @@ public: ## 其他语言版本 -Java: +### Java > 贪心法: @@ -242,11 +242,12 @@ class Solution { class Solution { public int maxProfit(int[] prices) { int[] dp = new int[2]; + // 记录一次交易,一次交易有买入卖出两种状态 + // 0代表持有,1代表卖出 dp[0] = -prices[0]; dp[1] = 0; // 可以参考斐波那契问题的优化方式 - // dp[0] 和 dp[1], 其实是第 0 天的数据 - // 所以我们从 i=1 开始遍历数组,一共有 prices.length 天, + // 我们从 i=1 开始遍历数组,一共有 prices.length 天, // 所以是 i<=prices.length for (int i = 1; i <= prices.length; i++) { // 前一天持有;或当天买入 @@ -263,7 +264,7 @@ class Solution { } ``` -Python: +### Python > 贪心法: ```python @@ -307,7 +308,8 @@ class Solution: return dp[(length-1) % 2][1] ``` -Go: +### Go + ```Go func maxProfit(prices []int) int { length:=len(prices) @@ -334,7 +336,7 @@ func max(a,b int)int { } ``` -JavaScript: +### JavaScript > 动态规划 From aa5498df7882df5cd42eedb34f96df9c662b0eb2 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:22:07 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BA=86122?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84java=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=85=B6=E5=AE=9E=E5=B0=B1=E6=98=AF=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 5b117563..e2464a03 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -171,15 +171,15 @@ class Solution { // 动态规划 // 优化空间 class Solution { public int maxProfit(int[] prices) { - int[] dp=new int[2]; + int[] dp = new int[2]; // 0表示持有,1表示卖出 - dp[0]=-prices[0]; - dp[1]=0; - for(int i=1; i<=prices.length; i++){ + 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[0] = Math.max(dp[0], dp[1] - prices[i-1]); // 前一天卖出; 或当天卖出,当天卖出,得先持有 - dp[1]=Math.max(dp[1], dp[0]+prices[i-1]); + dp[1] = Math.max(dp[1], dp[0] + prices[i-1]); } return dp[1]; } From 6bc248031953b214a2d08fc5180938048a099940 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:26:02 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BA=86123?= =?UTF-8?q?=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84=E7=A9=BA=E9=97=B4=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=9A=84java=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E4=BA=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0123.买卖股票的最佳时机III.md | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index db3f0278..3d67ec87 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -221,23 +221,23 @@ class Solution { // 版本二: 空间优化 class Solution { public int maxProfit(int[] prices) { - 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++){ + 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]); + 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[3]; } From 7575cfd43163d35096512c4ff75b40b578ff4f3a Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 16:28:54 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E4=BA=86188=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E9=97=AE=E9=A2=98=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E4=BC=98=E5=8C=96=E7=9A=84java=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0188.买卖股票的最佳时机IV.md | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 0e898b43..537ccd4a 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -165,7 +165,7 @@ public: ## 其他语言版本 -Java: +### Java ```java // 版本一: 三维 dp数组 @@ -228,9 +228,9 @@ class Solution { if(k == 0){ return 0; } - // 其实就是123题的扩展,123题只用记录2天的状态 - // 这里记录k天的状态就行了 - // 每天都有买入,卖出两个状态,所以要乘 2 + // 其实就是123题的扩展,123题只用记录2次交易的状态 + // 这里记录k次交易的状态就行了 + // 每次交易都有买入,卖出两个状态,所以要乘 2 int[] dp = new int[2 * k]; // 按123题解题格式那样,做一个初始化 for(int i = 0; i < dp.length / 2; i++){ @@ -246,15 +246,15 @@ class Solution { dp[j + 1] = Math.max(dp[j + 1], dp[j] + prices[i - 1]); } } - // 返回最后一天卖出状态的结果就行了 + // 返回最后一次交易卖出状态的结果就行了 return dp[dp.length - 1]; } } ``` - -Python: +### Python 版本一 + ```python class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: @@ -285,8 +285,9 @@ class Solution: dp[j] = max(dp[j],dp[j-1]+prices[i]) return dp[2*k] ``` -Go: +### Go 版本一: + ```go // 买卖股票的最佳时机IV 动态规划 // 时间复杂度O(kn) 空间复杂度O(kn) @@ -356,10 +357,7 @@ func max(a,b int)int{ } ``` - - - -Javascript: +### Javascript ```javascript // 方法一:动态规划 From 2517bb603948d44767f629fed00a459663723d4c Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 21:54:22 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E4=BA=86121=E3=80=81122?= =?UTF-8?q?=E3=80=81123=E3=80=81188md=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8C=E5=A5=BD=E5=BF=83=E5=B9=B2=E4=BA=86=E5=9D=8F=E4=BA=8B?= =?UTF-8?q?=EF=BC=8C=E6=8A=8A=E5=8D=A1=E5=93=A5=E6=A0=BC=E5=BC=8F=E6=94=B9?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E5=90=93=E6=AD=BB=EF=BC=8C=E8=B5=B6=E7=B4=A7?= =?UTF-8?q?=E6=94=B9=E8=BF=87=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 8 ++++---- problems/0122.买卖股票的最佳时机II.md | 14 ++++++++------ problems/0123.买卖股票的最佳时机III.md | 10 ++++------ problems/0188.买卖股票的最佳时机IV.md | 10 ++++++---- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 2815cd47..486e08bd 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -194,7 +194,7 @@ public: ## 其他语言版本 -### Java +Java: > 贪心法: @@ -264,7 +264,7 @@ class Solution { } ``` -### Python +Python: > 贪心法: ```python @@ -308,7 +308,7 @@ class Solution: return dp[(length-1) % 2][1] ``` -### Go +Go: ```Go func maxProfit(prices []int) int { @@ -336,7 +336,7 @@ func max(a,b int)int { } ``` -### JavaScript +JavaScript: > 动态规划 diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index e2464a03..1259aff7 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -131,7 +131,7 @@ public: ## 其他语言版本 -### Java +Java: ```java // 贪心思路 @@ -186,9 +186,8 @@ class Solution { } ``` +Python: - -### Python ```python class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -212,7 +211,8 @@ class Solution: return dp[-1][1] ``` -### Go +Go: + ```golang //贪心算法 func maxProfit(prices []int) int { @@ -248,7 +248,8 @@ func maxProfit(prices []int) int { } ``` -### Javascript +Javascript: + 贪心 ```Javascript var maxProfit = function(prices) { @@ -284,7 +285,8 @@ const maxProfit = (prices) => { }; ``` -### C +C: + ```c int maxProfit(int* prices, int pricesSize){ int result = 0; diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 3d67ec87..8fa3a8e0 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 // 版本一 @@ -244,7 +244,7 @@ class Solution { } ``` -### Python +Python: > 版本一: ```python @@ -311,9 +311,7 @@ func max(a,b int)int{ } ``` - - -### JavaScript +JavaScript: > 版本一: @@ -352,7 +350,7 @@ const maxProfit = prices => { }; ``` -### Go +Go: > 版本一: ```go diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 537ccd4a..615ebd5c 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -165,7 +165,7 @@ public: ## 其他语言版本 -### Java +Java: ```java // 版本一: 三维 dp数组 @@ -252,7 +252,8 @@ class Solution { } ``` -### Python +Python: + 版本一 ```python @@ -285,7 +286,8 @@ class Solution: dp[j] = max(dp[j],dp[j-1]+prices[i]) return dp[2*k] ``` -### Go +Go: + 版本一: ```go @@ -357,7 +359,7 @@ func max(a,b int)int{ } ``` -### Javascript +Javascript: ```javascript // 方法一:动态规划 From 09a19372ba0e805e0a5a776bd64434f26f3f72af Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Wed, 24 Nov 2021 22:11:33 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E4=B8=8D=E5=B0=8F=E5=BF=83=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=B0714=EF=BC=88=E8=B4=AA=E5=BF=83=EF=BC=89?= =?UTF-8?q?=E5=93=AA=E9=87=8C=E5=8E=BB=E4=BA=86=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E6=8A=8A=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E7=9A=84=E4=BB=A3=E7=A0=81=E9=87=8D=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=BF=9B=E4=BA=86=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E4=B8=AD=EF=BC=8C=E5=B7=B2=E6=9B=B4=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14.买卖股票的最佳时机含手续费.md | 16 ---------------- ...最佳时机含手续费(动态规划).md | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index 964d5ea9..f7ddeaf7 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -195,22 +195,6 @@ class Solution { // 动态规划 } ``` -```java -// 一维数组优化 -class Solution { - public int maxProfit(int[] prices, int fee) { - int[] dp = new int[2]; - 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] - fee); - } - return dp[1]; - } -} -``` - Python: diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 2b3416d4..700b8cde 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -134,6 +134,20 @@ public int maxProfit(int[] prices, int fee) { } return Math.max(dp[len - 1][0], dp[len - 1][1]); } + +// 一维数组优化 +class Solution { + public int maxProfit(int[] prices, int fee) { + int[] dp = new int[2]; + 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] - fee); + } + return dp[1]; + } +} ``` Python: From b18f227ffb572c08db29f54f66bbc149b5a68d11 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Thu, 25 Nov 2021 21:26:43 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E5=B0=86=E8=AF=AF=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0122=E8=B4=AA=E5=BF=83=E7=9A=84java=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E5=8A=A0=E5=88=B0=E4=BA=86?= =?UTF-8?q?122=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E5=A4=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0122.买卖股票的最佳时机II.md | 19 ------------ ...票的最佳时机II(动态规划).md | 29 +++++++++++-------- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 1259aff7..f6d5906a 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -167,25 +167,6 @@ 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: ```python diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index e701a821..8f03e88e 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -147,25 +147,30 @@ class Solution } return dp[n - 1][0]; // 卖出股票收益高于持有股票收益,因此取[0] } +} +``` - // 实现2:变量存储 - // 第一种方法需要用二维数组存储,有空间开销,其实关心的仅仅是前一天的状态,不关注更多的历史信息 - // 因此,可以仅保存前一天的信息存入 dp0、dp1 这 2 个变量即可 - // 时间复杂度:O(n),空间复杂度O(1) +```java +// 优化空间 +class Solution { public int maxProfit(int[] prices) { - int n = prices.length; - int dp0 = 0, dp1 = -prices[0]; // 定义变量,存储初始状态 - for (int i = 1; i < n; ++i) { - int newDp0 = Math.max(dp0, dp1 + prices[i]); // 第 i 天,没有股票 - int newDp1 = Math.max(dp1, dp0 - prices[i]); // 第 i 天,持有股票 - dp0 = newDp0; - dp1 = newDp1; + 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 dp0; + return dp[1]; } } ``` + + Python: > 版本一: From 1f33812b15367fc21c9498d77f286d4633d3a137 Mon Sep 17 00:00:00 2001 From: LiHua <1985390347@qq.com> Date: Thu, 25 Nov 2021 21:31:13 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=88=A0=E5=87=8F=E4=BA=86309=E4=B8=80?= =?UTF-8?q?=E7=BB=B4=E6=95=B0=E7=BB=84=E7=A9=BA=E9=97=B4=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=9A=84=E9=83=A8=E5=88=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0309.最佳买卖股票时机含冷冻期.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 8c6ab288..48106bd8 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -189,9 +189,8 @@ class Solution { // 一维数组优化 class Solution { public int maxProfit(int[] prices) { - // dp[2]和dp[3]用来存储冷冻期的数据 int[] dp=new int[4]; - // 0表示持有,1表示卖出 + dp[0] = -prices[0]; dp[1] = 0; for(int i = 1; i <= prices.length; i++){