Skip to content

Commit

Permalink
update 198 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 19, 2024
1 parent 7d6e0f6 commit 0cae20e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 18 deletions.
4 changes: 2 additions & 2 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 16 additions & 16 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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']
Expand All @@ -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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
49 changes: 49 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

}

0 comments on commit 0cae20e

Please sign in to comment.