From 8b2705f1317f96cd23ca123de3a4b8a207cd042e Mon Sep 17 00:00:00 2001 From: quyan Date: Wed, 12 Feb 2020 20:07:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3"=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=86=B7=E5=8D=B4=E6=9C=9F=E7=9A=84=E8=82=A1=E7=A5=A8=E4=BA=A4?= =?UTF-8?q?=E6=98=93"=E7=AE=97=E6=B3=95=EF=BC=8C=E5=B0=86=E6=84=8F?= =?UTF-8?q?=E4=B9=89=E4=B8=8D=E6=98=8E=E7=9A=84s1=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=B9=B6=E5=85=A5=E2=80=9Cbuy=E2=80=9D=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原解答状态转移图列出四个状态,实际上三个就够了(将意义不明的s1状态并入“buy”状态) --- notes/Leetcode 题解 - 动态规划.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/notes/Leetcode 题解 - 动态规划.md b/notes/Leetcode 题解 - 动态规划.md index 305174be..a8365af9 100644 --- a/notes/Leetcode 题解 - 动态规划.md +++ b/notes/Leetcode 题解 - 动态规划.md @@ -1055,7 +1055,10 @@ public int combinationSum4(int[] nums, int target) { 题目描述:交易之后需要有一天的冷却时间。 -

+ +该题为马尔可夫过程,分为A观望,B持股,C冷却三个状态 +状态转移图:A-(观望)->A, A-(买入)->B, B-(观望)->B, B-(卖出)->C, C-(冷却)->A +可用维特比算法求解 ```java public int maxProfit(int[] prices) { @@ -1063,19 +1066,17 @@ public int maxProfit(int[] prices) { return 0; } int N = prices.length; - int[] buy = new int[N]; - int[] s1 = new int[N]; - int[] sell = new int[N]; - int[] s2 = new int[N]; - s1[0] = buy[0] = -prices[0]; - sell[0] = s2[0] = 0; + int[] A = new int[N]; + int[] B = new int[N]; + int[] C = new int[N]; + A[0] = 0; + B[0] = C[0] = -prices[0]; for (int i = 1; i < N; i++) { - buy[i] = s2[i - 1] - prices[i]; - s1[i] = Math.max(buy[i - 1], s1[i - 1]); - sell[i] = Math.max(buy[i - 1], s1[i - 1]) + prices[i]; - s2[i] = Math.max(s2[i - 1], sell[i - 1]); + A[i] = Math.max(A[i - 1], C[i - 1]); + B[i] = Math.max(B[i - 1], A[i - 1] - prices[i]); + C[i] = B[i - 1] + prices[i]; } - return Math.max(sell[N - 1], s2[N - 1]); + return Math.max(A[N - 1], C[N - 1]); } ``` From 7858dacf7b0ff5116557379decf758be81583a8c Mon Sep 17 00:00:00 2001 From: quyan Date: Thu, 13 Feb 2020 18:41:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AE=80=E5=8C=96=E2=80=9D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E4=BA=A4=E6=98=93=E8=B4=B9=E7=94=A8=E7=9A=84=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E4=BA=A4=E6=98=93=E2=80=9C=E9=A2=98=E8=A7=A3=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84s?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原图画了四个状态实在没有必要,只需要两个状态 --- notes/Leetcode 题解 - 动态规划.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/notes/Leetcode 题解 - 动态规划.md b/notes/Leetcode 题解 - 动态规划.md index a8365af9..643b0dc7 100644 --- a/notes/Leetcode 题解 - 动态规划.md +++ b/notes/Leetcode 题解 - 动态规划.md @@ -1057,7 +1057,7 @@ public int combinationSum4(int[] nums, int target) { 该题为马尔可夫过程,分为A观望,B持股,C冷却三个状态 -状态转移图:A-(观望)->A, A-(买入)->B, B-(观望)->B, B-(卖出)->C, C-(冷却)->A +状态转移图:A-(观望)->A, A-(买入|-price)->B, B-(观望)->B, B-(卖出|+price)->C, C-(冷却)->A 可用维特比算法求解 ```java @@ -1099,24 +1099,22 @@ The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8. 题目描述:每交易一次,都要支付一定的费用。 -

+ +分为A观望,B持股,两个状态 +状态转移图:A-(观望)->A, A-(买入|-price)->B, B-(观望)->B, B-(卖出|+price|-fee)->A ```java public int maxProfit(int[] prices, int fee) { int N = prices.length; - int[] buy = new int[N]; - int[] s1 = new int[N]; - int[] sell = new int[N]; - int[] s2 = new int[N]; - s1[0] = buy[0] = -prices[0]; - sell[0] = s2[0] = 0; + int[] A = new int[N]; + int[] B = new int[N]; + A[0] = 0; + B[0] = -prices[0]; for (int i = 1; i < N; i++) { - buy[i] = Math.max(sell[i - 1], s2[i - 1]) - prices[i]; - s1[i] = Math.max(buy[i - 1], s1[i - 1]); - sell[i] = Math.max(buy[i - 1], s1[i - 1]) - fee + prices[i]; - s2[i] = Math.max(s2[i - 1], sell[i - 1]); + A[i] = Math.max(A[i - 1], B[i - 1] + prices[i] -fee); + B[i] = Math.max(A[i - 1] - prices[i], B[i - 1]); } - return Math.max(sell[N - 1], s2[N - 1]); + return A[N - 1]; } ```