From b1e8b17eb1bfd4e620b49037154693d4f9188a96 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Wed, 14 Jun 2023 18:30:56 -0400 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E4=B8=80=E7=B6=AD?= =?UTF-8?q?=E6=95=B8=E7=B5=84=E8=A7=A3=E6=B3=95=EF=BC=88=E5=BE=88=E5=8D=A1?= =?UTF-8?q?=E5=93=A5=E9=82=8F=E8=BC=AF=E4=B8=80=E8=87=B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原本的那個版本不知道在寫什麼 --- ...\344\275\263\346\227\266\346\234\272IV.md" | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 5bed5ecc5d..14f514a98f 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -227,7 +227,7 @@ class Solution { } } -//版本三:一维 dp数组 +//版本三:一维 dp数组 (下面有和卡哥邏輯一致的一維數組JAVA解法) class Solution { public int maxProfit(int k, int[] prices) { if(prices.length == 0){ @@ -259,6 +259,41 @@ class Solution { } } ``` +```JAVA +class Solution { + public int maxProfit(int k, int[] prices) { + + //edge cases + if(prices.length == 0 || k == 0) + return 0; + + + int dp[] = new int [k * 2 + 1]; + + //和卡哥邏輯一致,奇數天購入股票,故初始化只初始化奇數天。 + for(int i = 1; i < 2 * k + 1; i += 2){ + dp[i] = -prices[0]; + } + + for(int i = 1; i < prices.length; i++){ //i 從 1 開始,因爲第 i = 0 天已經透過初始化完成了。 + for(int j = 1; j < 2 * k + 1; j++){ //j 從 1 開始,因爲第 j = 0 天已經透過初始化完成了。 + //奇數天購買 + if(j % 2 == 1) + dp[j] = Math.max(dp[j], dp[j - 1] - prices[i]); + //偶數天賣出 + else + dp[j] = Math.max(dp[j], dp[j - 1] + prices[i]); + } + //打印DP數組 + //for(int x : dp) + // System.out.print(x +", "); + //System.out.println(); + } + //return 第2 * k次賣出的獲利。 + return dp[2 * k]; + } +} +``` Python: From 9c1988edec9d6eeca9db1bd558ad16410f40eaf6 Mon Sep 17 00:00:00 2001 From: Binbin Date: Thu, 15 Jun 2023 19:32:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9A=E5=B7=B2=E7=BB=8F=E5=A1=AB=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E4=B8=B2=20->=20=E5=B7=B2=E7=BB=8F=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\211\262\345\233\236\346\226\207\344\270\262.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 636cf59c4b..92fed58a2e 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -118,7 +118,7 @@ for (int i = startIndex; i < s.size(); i++) { continue; } backtracking(s, i + 1); // 寻找i+1为起始位置的子串 - path.pop_back(); // 回溯过程,弹出本次已经填在的子串 + path.pop_back(); // 回溯过程,弹出本次已经添加的子串 } ``` @@ -189,7 +189,7 @@ private: continue; } backtracking(s, i + 1); // 寻找i+1为起始位置的子串 - path.pop_back(); // 回溯过程,弹出本次已经填在的子串 + path.pop_back(); // 回溯过程,弹出本次已经添加的子串 } } bool isPalindrome(const string& s, int start, int end) { @@ -245,7 +245,7 @@ private: continue; } backtracking(s, i + 1); // 寻找i+1为起始位置的子串 - path.pop_back(); // 回溯过程,弹出本次已经填在的子串 + path.pop_back(); // 回溯过程,弹出本次已经添加的子串 } } void computePalindrome(const string& s) { @@ -437,7 +437,7 @@ class Solution: substring = s[startIndex:i + 1] path.append(substring) self.backtracking(s, i + 1, path, result, isPalindrome) # 寻找i+1为起始位置的子串 - path.pop() # 回溯过程,弹出本次已经填在的子串 + path.pop() # 回溯过程,弹出本次已经添加的子串 def computePalindrome(self, s, isPalindrome): for i in range(len(s) - 1, -1, -1): # 需要倒序计算,保证在i行时,i+1行已经计算好了 @@ -497,7 +497,7 @@ func dfs(s string, start int) { if isPalindrome(str) { // 是回文子串 path = append(path, str) dfs(s, i+1) // 寻找i+1为起始位置的子串 - path = path[:len(path)-1] // 回溯过程,弹出本次已经填在的子串 + path = path[:len(path)-1] // 回溯过程,弹出本次已经添加的子串 } } } From 3becfab93f477723e4c3aee75da45989bebd0bb6 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 15 Jun 2023 17:58:09 -0400 Subject: [PATCH 3/3] =?UTF-8?q?=E8=AA=AA=E6=98=8EJAVA=20code=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=B3=A8=E6=84=8F=E7=9A=84=E5=9C=B0=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 81f02ace98..8cc270ec64 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -177,6 +177,7 @@ Java: dp[i] = 1; } int res = 1; + //可以注意到,這邊的 i 是從 0 開始,所以會出現和卡哥的C++ code有差異的地方,在一些地方會看到有 i + 1 的偏移。 for (int i = 0; i < nums.length - 1; i++) { if (nums[i + 1] > nums[i]) { dp[i + 1] = dp[i] + 1;