From b660360285c0fbf520106b7afd03f049bc0b6300 Mon Sep 17 00:00:00 2001 From: yujiahan2018 Date: Sat, 20 May 2023 19:29:45 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\255\220\346\225\260\347\273\204.md" | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 08be67326e..31782d5d6c 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -198,7 +198,57 @@ public: 而且为了让 `if (dp[i][j] > result) result = dp[i][j];` 收集到全部结果,两层for训练一定从0开始遍历,这样需要加上 `&& i > 0 && j > 0`的判断。 -相对于版本一来说还是多写了不少代码。而且逻辑上也复杂了一些。 优势就是dp数组的定义,更直观一点。 +对于基础不牢的小白来说,在推导出转移方程后可能疑惑上述代码为什么要从`i=0,j=0`遍历而不是从`i=1,j=1`开始遍历,原因在于这里如果不是从`i=0,j=0`位置开始遍历,会漏掉如下样例结果: +```txt +nums1 = [70,39,25,40,7] +nums2 = [52,20,67,5,31] +``` + +当然,如果你愿意也可以使用如下代码,与上面那个c++是同一思路: +```java +class Solution { + public int findLength(int[] nums1, int[] nums2) { + int len1 = nums1.length; + int len2 = nums2.length; + int[][] result = new int[len1][len2]; + + int maxresult = Integer.MIN_VALUE; + + for(int i=0;i Date: Sat, 20 May 2023 19:31:51 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\345\256\266\345\212\253\350\210\215.md" | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 6e682ec39e..7c8ba4db36 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -141,7 +141,36 @@ class Solution { } } -// 空间优化 dp数组只存与计算相关的两次数据 +// 使用滚动数组思想,优化空间 +// 分析本题可以发现,所求结果仅依赖于前两种状态,此时可以使用滚动数组思想将空间复杂度降低为3个空间 +class Solution { + public int rob(int[] nums) { + + int len = nums.length; + + if (len == 0) return 0; + else if (len == 1) return nums[0]; + else if (len == 2) return Math.max(nums[0],nums[1]); + + + int[] result = new int[3]; //存放选择的结果 + result[0] = nums[0]; + result[1] = Math.max(nums[0],nums[1]); + + + for(int i=2;i nums[1] ? nums[0] : nums[1]; + dp[1] = Math.max(nums[0],nums[1]); int res = 0; // 遍历 for (int i = 2; i < nums.length; i++) { - res = (dp[0] + nums[i]) > dp[1] ? (dp[0] + nums[i]) : dp[1]; + res = Math.max((dp[0] + nums[i]) , dp[1] ); dp[0] = dp[1]; dp[1] = res; } From 6498ee65f7d9d38e59d3e338bdbec9a79cb6a438 Mon Sep 17 00:00:00 2001 From: zdz <2549626703@qq.com> Date: Tue, 23 May 2023 22:36:43 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix=EF=BC=9A0123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?III=20=3D=E3=80=8B=E9=94=99=E8=AF=AF=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\346\234\200\344\275\263\346\227\266\346\234\272III.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0123.\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\272III.md" "b/problems/0123.\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\272III.md" index af6870d4cc..6ac9a576ea 100644 --- "a/problems/0123.\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\272III.md" +++ "b/problems/0123.\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\272III.md" @@ -242,9 +242,9 @@ class Solution { for (int i = 1; i < len; i++) { dp[i][1] = Math.max(dp[i - 1][1], -prices[i]); - dp[i][2] = Math.max(dp[i - 1][2], dp[i][1] + prices[i]); - dp[i][3] = Math.max(dp[i - 1][3], dp[i][2] - prices[i]); - dp[i][4] = Math.max(dp[i - 1][4], dp[i][3] + prices[i]); + dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]); + dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]); + dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]); } return dp[len - 1][4]; From a4dcbe67217574e6fd197ad0c9598177faec79e9 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Fri, 26 May 2023 18:05:09 -0400 Subject: [PATCH 4/4] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E4=BA=8C=E7=B6=AD"?= =?UTF-8?q?=E6=95=B4=E6=95=B8"=E6=95=B8=E7=B5=84=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E3=80=81=E5=84=AA=E5=8C=96=E4=B8=80=E7=B6=AD=E6=95=B8?= =?UTF-8?q?=E7=B5=84=E7=9A=84=E8=A7=A3=E6=B3=95(=E5=89=AA=E6=9E=9D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增java二維"整數"數組的解法,原本是用boolean數組,有一點繞,故附上二維"整數"數組。 2. 優化一維數組的解法(剪枝),在每一次內層循環完後,立刻檢查dp[target] == target。實測時間複雜度: 26ms -> 20ms --- ...11\345\222\214\345\255\220\351\233\206.md" | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 8115e18ea9..54d9061217 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -218,8 +218,12 @@ class Solution { for(int i = 0; i < n; i++) { for(int j = target; j >= nums[i]; j--) { //物品 i 的重量是 nums[i],其价值也是 nums[i] - dp[j] = Math.max(dp[j], dp[j-nums[i]] + nums[i]); + dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); } + + //剪枝一下,每一次完成內層的for-loop,立即檢查是否dp[target] == target,優化時間複雜度(26ms -> 20ms) + if(dp[target] == target) + return true; } return dp[target] == target; } @@ -294,6 +298,61 @@ false true false false false true true false false false false false false true false false false true true false false false false true false true false false false true true false false false true true ``` +二維數組整數版本 +```Java +class Solution { + public boolean canPartition(int[] nums) { + //using 2-D DP array. + int len = nums.length; + //check edge cases; + if(len == 0) + return false; + + int sum = 0; + for (int num : nums) + sum += num; + //we only deal with even numbers. If sum is odd, return false; + if(sum % 2 == 1) + return false; + + int target = sum / 2; + int[][] dp = new int[nums.length][target + 1]; + + // for(int j = 0; j <= target; j++){ + // if(j < nums[0]) + // dp[0][j] = 0; + // else + // dp[0][j] = nums[0]; + // } + + //initialize dp array + for(int j = nums[0]; j <= target; j++){ + dp[0][j] = nums[0]; + } + + for(int i = 1; i < len; i++){ + for(int j = 0; j <= target; j++){ + if (j < nums[i]) + dp[i][j] = dp[i - 1][j]; + else + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); + } + } + + //print out DP array + // for(int x : dp){ + // System.out.print(x + ","); + // } + // System.out.print(" "+i+" row"+"\n"); + return dp[len - 1][target] == target; + } +} +//dp数组的打印结果 for test case 1. +0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +0, 1, 1, 1, 1, 5, 6, 6, 6, 6, 6, 6, +0, 1, 1, 1, 1, 5, 6, 6, 6, 6, 6, 11, +0, 1, 1, 1, 1, 5, 6, 6, 6, 6, 10, 11, +``` ### Python: ```python