From 0cae20e230c612020724ea12236ddf88f870b5fe Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 19 May 2024 17:50:38 +0800 Subject: [PATCH] update 198 java, progress --- data/progress.txt | 4 +- data/to_review.txt | 32 ++++++------ .../DynamicProgramming/HouseRobber.java | 46 +++++++++++++++++ .../src/main/java/dev/workspace3.java | 49 +++++++++++++++++++ 4 files changed, 113 insertions(+), 18 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index 887bb773..7e968c0b 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,5 +1,5 @@ -20240519: 207,79,206 -20240518: 213(todo),198(todo),212(todo),211,338,208(again) +20240519: 207,79,206,213(todo),198 +20240518: 212(todo),211,338,208(again) 20240517: 347,253(todo),91(todo),217 20240516: 226,98,253(todo) 20240515: 104,230,102,100 diff --git a/data/to_review.txt b/data/to_review.txt index 19333b9e..908cc399 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,5 +1,5 @@ -2024-07-13 -> ['207,79,206'] -2024-07-12 -> ['213(todo),198(todo),212(todo),211,338,208(again)'] +2024-07-13 -> ['207,79,206,213(todo),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)'] 2024-07-09 -> ['104,230,102,100'] @@ -8,8 +8,8 @@ 2024-07-06 -> ['371'] 2024-07-05 -> ['121,252'] 2024-07-04 -> ['125'] -2024-06-22 -> ['207,79,206'] -2024-06-21 -> ['213(todo),198(todo),212(todo),211,338,208(again)'] +2024-06-22 -> ['207,79,206,213(todo),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)'] 2024-06-18 -> ['104,230,102,100'] @@ -18,28 +18,28 @@ 2024-06-15 -> ['371'] 2024-06-14 -> ['121,252'] 2024-06-13 -> ['125'] -2024-06-09 -> ['207,79,206'] -2024-06-08 -> ['213(todo),198(todo),212(todo),211,338,208(again)'] +2024-06-09 -> ['207,79,206,213(todo),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)'] 2024-06-05 -> ['104,230,102,100'] 2024-06-04 -> ['105,106'] 2024-06-03 -> ['242,235'] 2024-06-02 -> ['371'] -2024-06-01 -> ['207,79,206', '121,252'] -2024-05-31 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '125'] +2024-06-01 -> ['207,79,206,213(todo),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', '105,106'] -2024-05-26 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '242,235'] +2024-05-27 -> ['207,79,206,213(todo),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', '226,98,253(todo)', '121,252'] -2024-05-23 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '104,230,102,100', '125'] -2024-05-22 -> ['207,79,206', '347,253(todo),91(todo),217', '105,106'] -2024-05-21 -> ['207,79,206', '213(todo),198(todo),212(todo),211,338,208(again)', '226,98,253(todo)', '242,235'] -2024-05-20 -> ['207,79,206', '213(todo),198(todo),212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '104,230,102,100', '371'] -2024-05-19 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252'] +2024-05-24 -> ['207,79,206,213(todo),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-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'] 2024-05-16 -> ['104,230,102,100', '105,106', '242,235', '121,252'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber.java index e207c238..587bfe0f 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber.java +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber.java @@ -34,6 +34,52 @@ public int rob(int[] nums) { return nums[nums.length-1]; } + // V0' + // IDEA : DP + public int rob_(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]); + } + + // init dp + int[] dp = new int[nums.length]; + + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); // NOTE !!! dp[1] comes from biggest option (which is Math.max(nums[0], nums[1])) + + // 1, 2, 3, ... k-2, k-1, k + // dp[k] = max(dp(k-2) + k, dp(k-1)) + /** + * 2,1,1,2 + * + * -> dp[0] = 2 + * -> dp[1] = 2 + * + * -> dp[2] = max(dp[0]+nums[2], dp[1]) = 3 + * -> dp[3] = max(dp[1]+nums[3], dp[2]) = 4 + * + */ + for (int i = 2; i < nums.length; i++){ + dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]); + } + + return dp[nums.length-1]; + } + // V1 // IDEA : DP // https://leetcode.com/problems/house-robber/solutions/4600148/beats-100-c-java-python-js-explained-with-video-dynamic-programming-space-optimized/ diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 22f371b4..d794b98d 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -973,4 +973,53 @@ public ListNode reverseList(ListNode head) { return prev; } + // LC 198 + // dp + public int rob_(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 + // dp[k] = max(dp(k-2) + k, dp(k-1)) + /** + * 2,1,1,2 + * + * -> dp[0] = 2 + * -> dp[1] = 2 + * + * -> dp[2] = max(dp[0]+nums[2], dp[1]) = 3 + * -> dp[3] = max(dp[1]+nums[3], dp[2]) = 4 + * + */ + 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]; + } + }