diff --git a/README.md b/README.md index cc6371b5..6f5e9865 100644 --- a/README.md +++ b/README.md @@ -1021,7 +1021,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) | _O(n)_| _O(1)_| Medium|brute force, recursion, dp, Curated Top 75, google, amazon| AGAIN (not start) +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) 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 6cd05ee2..2c61f7b0 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240310: 213 20240309: 207(again!!!),208,211(again),212 20240308: 152,153(again),190,191,198,200,206(again) 20240307: 139(again),141,143(again) diff --git a/data/to_review.txt b/data/to_review.txt index 88bbb6a1..a05f092a 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,3 +1,4 @@ +2024-05-04 -> ['213'] 2024-05-03 -> ['207(again!!!),208,211(again),212'] 2024-05-02 -> ['152,153(again),190,191,198,200,206(again)'] 2024-05-01 -> ['139(again),141,143(again)'] @@ -8,6 +9,7 @@ 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] +2024-04-13 -> ['213'] 2024-04-12 -> ['207(again!!!),208,211(again),212'] 2024-04-11 -> ['152,153(again),190,191,198,200,206(again)'] 2024-04-10 -> ['139(again),141,143(again)'] @@ -18,6 +20,7 @@ 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] 2024-04-01 -> ['1,3,5,4,19'] +2024-03-31 -> ['213'] 2024-03-30 -> ['207(again!!!),208,211(again),212'] 2024-03-29 -> ['152,153(again),190,191,198,200,206(again)'] 2024-03-28 -> ['139(again),141,143(again)'] @@ -25,18 +28,19 @@ 2024-03-26 -> ['100,102,104,105(again)'] 2024-03-25 -> ['73,76,79(again),91,25'] 2024-03-24 -> ['55(again),56,62,70'] +2024-03-23 -> ['213'] 2024-03-22 -> ['207(again!!!),208,211(again),212'] 2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54'] 2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)'] 2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19'] -2024-03-18 -> ['100,102,104,105(again)'] +2024-03-18 -> ['213', '100,102,104,105(again)'] 2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25'] 2024-03-16 -> ['152,153(again),190,191,198,200,206(again)', '55(again),56,62,70'] -2024-03-15 -> ['139(again),141,143(again)'] +2024-03-15 -> ['213', '139(again),141,143(again)'] 2024-03-14 -> ['207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)'] -2024-03-13 -> ['152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54'] -2024-03-12 -> ['207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] -2024-03-11 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19'] +2024-03-13 -> ['213', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54'] +2024-03-12 -> ['213', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-11 -> ['213', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)'] 2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25'] 2024-03-08 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java new file mode 100644 index 00000000..e68e69ac --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java @@ -0,0 +1,108 @@ +package LeetCodeJava.DynamicProgramming; + +// https://leetcode.com/problems/house-robber-ii/ + +import java.util.Arrays; + +public class HouseRobber2 { + + // V0 + // IDEA : DP, LC 198 + // https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Dynamic_Programming/house-robber-ii.py + public int rob(int[] nums) { + + if (nums.length == 1) { + return nums[0]; + } + + if (nums.length == 2) { + return Math.max(nums[0], nums[1]); + } + + // Consider the scenario where the first and last houses are adjacent + // so either + // -> we rob first and last 2 idx + // -> or rob 2nd and last idx + int max1 = robRange(nums, 0, nums.length - 2); + int max2 = robRange(nums, 1, nums.length - 1); + + return Math.max(max1, max2); + } + + // 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++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); + } + + return dp[end]; + } + + // V1 + // https://leetcode.com/problems/house-robber-ii/solutions/4844002/beats-100-simple-to-understand/ + public int robber(int[] nums) { + int n=nums.length; + int prev=nums[0]; + int prev2=0; + for(int i=1;i1) pick+=prev2; + + int notpick=prev; + int curr=Math.max(pick, notpick); + prev2=prev; + prev=curr; + } + return prev; + } + public int rob_2(int[] nums) { + int n=nums.length; + if(n==1) return nums[0]; + int[] ind1=new int[n-1]; + int[] ind2=new int[n-1]; + for(int i=0;i map=new HashMap<>(); +// if(ind==0) +// return nums[0]; +// else +// return Math.max(f(ind-1,nums,0,map),f(ind,nums,nums[ind],map)); +// } +// public static int f(int i,int nums[],int curr,HashMap map){ +// Pair key=new Pair(i,curr); +// if(map.containsKey(key)) +// return map.get(key); +// if(i<0) +// return 0; +// if(i==0){ +// if(curr==0) +// return nums[0]; +// else +// return 0; +// } +// int take=f(i-2,nums,curr,map)+nums[i]; +// int nottake=f(i-1,nums,curr,map); +// map.put(key,Math.max(take,nottake)); +// return Math.max(take,nottake); +// } + +} diff --git a/leetcode_python/Dynamic_Programming/house-robber-ii.py b/leetcode_python/Dynamic_Programming/house-robber-ii.py index 34c718a9..c2fd83af 100644 --- a/leetcode_python/Dynamic_Programming/house-robber-ii.py +++ b/leetcode_python/Dynamic_Programming/house-robber-ii.py @@ -38,7 +38,13 @@ # V1 # IDEA : BRUTE FORCE # https://leetcode.com/problems/house-robber-ii/discuss/230657/Python-solution -# Since nums[0] and nums[-1] cannot be robbed simultaneously, The robber has to rob houses in nums[:-1] or in nums[1:], whichever is larger. Therefore, the problem reduces to two LC 198. House Robber I problems, which have already been solved. +# IDEA : +# Since nums[0] and nums[-1] cannot be robbed simultaneously, +# The robber has to rob houses in nums[:-1] or in nums[1:], +# whichever is larger. Therefore, the problem reduces to two LC 198. +# House Robber I problems, which have already been solved. +# -> so same as LC 198 (House robber) +# -> just need to implement method on nums[:-1] or nums[1:], and return max class Solution: def rob(self, nums): """