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] // 回溯过程,弹出本次已经添加的子串 } } } 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円272円IV.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円272円IV.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円272円IV.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円272円IV.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: 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;

AltStyle によって変換されたページ (->オリジナル) /