diff --git "a/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" "b/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" index 5dde64098d..8c85985fba 100644 --- "a/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" +++ "b/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" @@ -133,12 +133,13 @@ Java: class Solution { public int climbStairs(int n) { int[] dp = new int[n + 1]; - int m = 2; + int m = 2; //有兩個物品:itme1重量爲一,item2重量爲二 dp[0] = 1; for (int i = 1; i <= n; i++) { // 遍历背包 for (int j = 1; j <= m; j++) { //遍历物品 - if (i>= j) dp[i] += dp[i - j]; + if (i>= j) //當前的背包容量 大於 物品重量的時候,我們才需要記錄當前的這個裝得方法(方法數+) + dp[i] += dp[i - j]; } } diff --git "a/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" "b/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" index 67f6d564c7..d10e61b77a 100644 --- "a/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" +++ "b/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" @@ -200,7 +200,33 @@ class Solution { } } ``` - +```java +//using 2*4 array for space optimization +//這裡稍微說一下,我在LeetCode提交的時候,2*4 2-D array的performance基本上和下面的1-D array performance差不多 +//都是time: 1ms, space: 40.X MB (其實 length*4 的 2-D array也僅僅是space:41.X MB,看起來不多) +//股票累的DP題目大致上都是這樣,就當作是一個延伸就好了。真的有人問如何優化,最起碼有東西可以講。 +class Solution { + /** + 1. [i][0] holding the stock + 2. [i][1] after cooldown but stil not buing the stock + 3. [i][2] selling the stock + 4. [i][3] cooldown + */ + public int maxProfit(int[] prices) { + int len = prices.length; + int dp[][] = new int [2][4]; + dp[0][0] = -prices[0]; + + for(int i = 1; i < len; i++){ + dp[i % 2][0] = Math.max(Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]), dp[(i - 1) % 2][3] - prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][3]); + dp[i % 2][2] = dp[(i - 1) % 2][0] + prices[i]; + dp[i % 2][3] = dp[(i - 1) % 2][2]; + } + return Math.max(Math.max(dp[(len - 1) % 2][1], dp[(len - 1) % 2][2]), dp[(len - 1) % 2][3]); + } +} +``` ```java // 一维数组优化 class Solution { diff --git "a/problems/0714.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円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" "b/problems/0714.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円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" index 12789934ff..1443f14710 100644 --- "a/problems/0714.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円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" +++ "b/problems/0714.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円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" @@ -154,6 +154,25 @@ class Solution { return dp[1]; } } +```Java +//使用 2*2 array +class Solution { + public int maxProfit(int[] prices, int fee) { + int dp[][] = new int[2][2]; + int len = prices.length; + //[i][0] = holding the stock + //[i][1] = not holding the stock + dp[0][0] = -prices[0]; + + for(int i = 1; i < len; i++){ + dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i] - fee); + } + + return dp[(len - 1) % 2][1]; + } +} +``` ``` Python:

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