Skip to content

Commit

Permalink
update 213 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 19, 2024
1 parent 0cae20e commit 52775c7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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)']
Expand All @@ -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)']
Expand All @@ -18,27 +18,27 @@
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)']
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,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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
}

Expand Down
63 changes: 63 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

}

0 comments on commit 52775c7

Please sign in to comment.