From 52775c77a4f1acb231e4b73e1170dc8cb2e5c9fb Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 19 May 2024 18:24:38 +0800 Subject: [PATCH] update 213 java, progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +++--- .../DynamicProgramming/HouseRobber2.java | 12 +++- .../src/main/java/dev/workspace3.java | 63 +++++++++++++++++++ 5 files changed, 83 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index fdb223da..21de2871 100644 --- a/README.md +++ b/README.md @@ -1022,7 +1022,7 @@ 123| [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Python](./leetcode_python/Dynamic_Programming/best-time-to-buy-and-sell-stock-iii.py) | _O(m * n)_ | _O(n)_ | Hard |`amazon`, uber, apple| AGAIN* (not start) (1) 152| [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Python](./leetcode_python/Dynamic_Programming/maximum-product-subarray.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximumProductSubarray.java)| _O(n)_ | _O(1)_ | Medium |dp, brute force, Curated Top 75, google, amazon, linkedin| AGAIN***** (1) 198| [House Robber](https://leetcode.com/problems/house-robber/)| [Python](./leetcode_python/Dynamic_Programming/house-robber.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber.java) | _O(n)_ | _O(1)_ | Easy|Curated Top 75, dp basic,`amazon`| AGAIN*** (2) -213| [House Robber II](https://leetcode.com/problems/house-robber-ii/)| [Python](./leetcode_python/Dynamic_Programming/house-robber-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java) | _O(n)_| _O(1)_| Medium|brute force, recursion, dp, Curated Top 75, google, amazon| AGAIN (2) +213| [House Robber II](https://leetcode.com/problems/house-robber-ii/)| [Python](./leetcode_python/Dynamic_Programming/house-robber-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java) | _O(n)_| _O(1)_| Medium|brute force, recursion, dp, Curated Top 75, google, amazon| OK**** (3) (but again) 221| [Maximal Square](https://leetcode.com/problems/maximal-square/)|[Python](./leetcode_python/Dynamic_Programming/maximal-square.py) | _O(n^2)_ | _O(n)_ | Medium | EPI, dp, `amazon`,`fb` | AGAIN** (3) (not start) 256| [Paint House](https://leetcode.com/problems/paint-house/) | [Python](./leetcode_python/Dynamic_Programming/paint-house.py) | _O(n)_| _O(1)_| Medium |🔒| AGAIN (not start) 276| [Paint Fence](https://leetcode.com/problems/paint-fence/) | [Python](./leetcode_python/Dynamic_Programming/paint-fence.py) | _O(n)_| _O(1)_| Easy |🔒| AGAIN diff --git a/data/progress.txt b/data/progress.txt index 7e968c0b..90645d2f 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240519: 207,79,206,213(todo),198 +20240519: 207,79,206,213,198 20240518: 212(todo),211,338,208(again) 20240517: 347,253(todo),91(todo),217 20240516: 226,98,253(todo) diff --git a/data/to_review.txt b/data/to_review.txt index 908cc399..49cc38e9 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-07-13 -> ['207,79,206,213(todo),198'] +2024-07-13 -> ['207,79,206,213,198'] 2024-07-12 -> ['212(todo),211,338,208(again)'] 2024-07-11 -> ['347,253(todo),91(todo),217'] 2024-07-10 -> ['226,98,253(todo)'] @@ -8,7 +8,7 @@ 2024-07-06 -> ['371'] 2024-07-05 -> ['121,252'] 2024-07-04 -> ['125'] -2024-06-22 -> ['207,79,206,213(todo),198'] +2024-06-22 -> ['207,79,206,213,198'] 2024-06-21 -> ['212(todo),211,338,208(again)'] 2024-06-20 -> ['347,253(todo),91(todo),217'] 2024-06-19 -> ['226,98,253(todo)'] @@ -18,7 +18,7 @@ 2024-06-15 -> ['371'] 2024-06-14 -> ['121,252'] 2024-06-13 -> ['125'] -2024-06-09 -> ['207,79,206,213(todo),198'] +2024-06-09 -> ['207,79,206,213,198'] 2024-06-08 -> ['212(todo),211,338,208(again)'] 2024-06-07 -> ['347,253(todo),91(todo),217'] 2024-06-06 -> ['226,98,253(todo)'] @@ -26,19 +26,19 @@ 2024-06-04 -> ['105,106'] 2024-06-03 -> ['242,235'] 2024-06-02 -> ['371'] -2024-06-01 -> ['207,79,206,213(todo),198', '121,252'] +2024-06-01 -> ['207,79,206,213,198', '121,252'] 2024-05-31 -> ['212(todo),211,338,208(again)', '125'] 2024-05-30 -> ['347,253(todo),91(todo),217'] 2024-05-29 -> ['226,98,253(todo)'] 2024-05-28 -> ['104,230,102,100'] -2024-05-27 -> ['207,79,206,213(todo),198', '105,106'] +2024-05-27 -> ['207,79,206,213,198', '105,106'] 2024-05-26 -> ['212(todo),211,338,208(again)', '242,235'] 2024-05-25 -> ['347,253(todo),91(todo),217', '371'] -2024-05-24 -> ['207,79,206,213(todo),198', '226,98,253(todo)', '121,252'] +2024-05-24 -> ['207,79,206,213,198', '226,98,253(todo)', '121,252'] 2024-05-23 -> ['212(todo),211,338,208(again)', '104,230,102,100', '125'] -2024-05-22 -> ['207,79,206,213(todo),198', '347,253(todo),91(todo),217', '105,106'] -2024-05-21 -> ['207,79,206,213(todo),198', '212(todo),211,338,208(again)', '226,98,253(todo)', '242,235'] -2024-05-20 -> ['207,79,206,213(todo),198', '212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '104,230,102,100', '371'] +2024-05-22 -> ['207,79,206,213,198', '347,253(todo),91(todo),217', '105,106'] +2024-05-21 -> ['207,79,206,213,198', '212(todo),211,338,208(again)', '226,98,253(todo)', '242,235'] +2024-05-20 -> ['207,79,206,213,198', '212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '104,230,102,100', '371'] 2024-05-19 -> ['212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252'] 2024-05-18 -> ['347,253(todo),91(todo),217', '226,98,253(todo)', '104,230,102,100', '242,235', '125'] 2024-05-17 -> ['226,98,253(todo)', '104,230,102,100', '105,106', '371'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java index e1d04710..6e328dee 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java @@ -19,11 +19,15 @@ public int rob(int[] nums) { return Math.max(nums[0], nums[1]); } - // Consider the scenario where the first and last houses are adjacent + // Consider the scenario where the first and last houses are adjacent (nums is a "circular" array) // so either - // -> we rob first and last 2 idx - // -> or rob 2nd and last idx + // + // -> case 1) we rob first and last 2 idx (e.g. 0 idx, and k-2 idx) + // -> case 2) or rob 2nd and last idx (e.g. 1 idx, and k-1 idx) + + // case 1) int max1 = robRange(nums, 0, nums.length - 2); + // case 2) int max2 = robRange(nums, 1, nums.length - 1); return Math.max(max1, max2); @@ -32,11 +36,13 @@ public int rob(int[] nums) { // NOTE !!! define robRange(int[] nums, int start, int end) // with start, end idx public int robRange(int[] nums, int start, int end) { + int[] dp = new int[nums.length]; dp[start] = nums[start]; dp[start + 1] = Math.max(nums[start], nums[start + 1]); for (int i = start + 2; i <= end; i++) { + // still the same dp logic as LC 198 dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); } diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index d794b98d..92f84041 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -1022,4 +1022,67 @@ public int rob_(int[] nums) { return dp[nums.length-1]; } + // LC 213 + public int rob_2(int[] nums) { + + if (nums.length <= 3){ + + if (nums.length == 0){ + return 0; + } + + if (nums.length == 1){ + return nums[0]; + } + + if (nums.length == 2){ + return Math.max(nums[0], nums[1]); + } + + return Math.max(nums[0]+nums[2], nums[1]); + } + + int[] dp = new int[nums.length]; + + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + /** + * + * // 1, 2, 3, ... k-2, k-1, k + * // case 1) take dp[1] + * + * dp[k] = max(dp(k-2), dp(k-1)) + * + * // case 2) NOT take dp[1] + * + * dp[0] = 0 + * dp[1] = nums[1] + * dp[k] = max(dp(k-2) + nums[k], dp(k-1)) + * + * + * nums = [1,2,3,1] + * + * case 1) + * dp[0] = 1 + * dp[1] = 2 + * dp[2] = 4 + * dp[3] = + * max(dp(k-2), dp(k-1)) = 4 + * or + * max(dp(k-2) + k, dp(k-1)) = 3 + * + */ + + for (int i = 2; i < nums.length; i++){ + dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]); + } + + System.out.println("dp = " + dp.toString()); + for (int x : dp){ + System.out.println(x); + } + return dp[nums.length-1]; + } + }