Skip to content

Commit

Permalink
update java 55, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 24, 2024
1 parent e2a89ff commit 2382e37
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
011| [Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Python](./leetcode_python/Greedy/container-with-most-water.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/ContainerWithMostWater.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, good basics, `two pointers`,`fb`, amazon| OK*** (3) (but again !!)
045| [Jump Game II](https://leetcode.com/problems/jump-game-ii/)|[Python](./leetcode_python/Greedy/jump-game-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame2.java) | _O(n)_ | _O(1)_| Medium|Greedy, google, apple, fb, tesla, `amazon`| AGAIN******** (4) (MUST)
055| [Jump Game](https://leetcode.com/problems/jump-game/) |[Python](./leetcode_python/Greedy/jump-game.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java) | _O(n)_ | _O(1)_| Medium|Curated Top 75, good trick, backtrack, Greedy, DP, `amazon`| AGAIN******** (7)
055| [Jump Game](https://leetcode.com/problems/jump-game/) |[Python](./leetcode_python/Greedy/jump-game.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java) | _O(n)_ | _O(1)_| Medium|Curated Top 75, good trick, backtrack, Greedy, DP, `amazon`| AGAIN******** (8)
084| [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Python](./leetcode_python/Greedy/largest-rectangle-in-histogram.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/LargestRectangleInHistogram.java)| _O(n)_ | _O(1)_| Hard |top 100 like, brute force, divide and conquer, good trick, `stack`, LC 085, amazon, fb| AGAIN**** (1)
122| [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Python](./leetcode_python/Greedy/best-time-to-buy-and-sell-stock-ii.py) | _O(n)_ | _O(1)_ | Easy |compare with `#309 Best Time to Buy and Sell Stock with Cooldown `, `#714 Best Time to Buy and Sell Stock with Transaction Fee`, `amazon`| OK* (2)
134| [Gas Station](https://leetcode.com/problems/gas-station/)| [Python](./leetcode_python/Greedy/gas-station.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java) | _O(n)_ | _O(1)_ | Medium|trick, greedy,`amazon`| AGAIN****** (5)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240524: 55
20240523: 56
20240522: 62,572,57
20240521: 322,191,190
Expand Down
16 changes: 9 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-07-18 -> ['55']
2024-07-17 -> ['56']
2024-07-16 -> ['62,572,57']
2024-07-15 -> ['322,191,190']
Expand All @@ -12,6 +13,7 @@
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-27 -> ['55']
2024-06-26 -> ['56']
2024-06-25 -> ['62,572,57']
2024-06-24 -> ['322,191,190']
Expand All @@ -24,27 +26,27 @@
2024-06-17 -> ['105,106']
2024-06-16 -> ['242,235']
2024-06-15 -> ['371']
2024-06-14 -> ['121,252']
2024-06-14 -> ['55', '121,252']
2024-06-13 -> ['56', '125']
2024-06-12 -> ['62,572,57']
2024-06-11 -> ['322,191,190']
2024-06-10 -> ['73,200,70,323(again)']
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-06 -> ['55', '226,98,253(todo)']
2024-06-05 -> ['56', '104,230,102,100']
2024-06-04 -> ['62,572,57', '105,106']
2024-06-03 -> ['322,191,190', '242,235']
2024-06-02 -> ['73,200,70,323(again)', '371']
2024-06-01 -> ['207,79,206,213,198', '121,252']
2024-06-01 -> ['55', '207,79,206,213,198', '121,252']
2024-05-31 -> ['56', '212(todo),211,338,208(again)', '125']
2024-05-30 -> ['62,572,57', '347,253(todo),91(todo),217']
2024-05-29 -> ['322,191,190', '226,98,253(todo)']
2024-05-29 -> ['55', '322,191,190', '226,98,253(todo)']
2024-05-28 -> ['56', '73,200,70,323(again)', '104,230,102,100']
2024-05-27 -> ['62,572,57', '207,79,206,213,198', '105,106']
2024-05-26 -> ['56', '322,191,190', '212(todo),211,338,208(again)', '242,235']
2024-05-25 -> ['56', '62,572,57', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371']
2024-05-27 -> ['55', '62,572,57', '207,79,206,213,198', '105,106']
2024-05-26 -> ['55', '56', '322,191,190', '212(todo),211,338,208(again)', '242,235']
2024-05-25 -> ['55', '56', '62,572,57', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371']
2024-05-24 -> ['56', '62,572,57', '322,191,190', '207,79,206,213,198', '226,98,253(todo)', '121,252']
2024-05-23 -> ['62,572,57', '322,191,190', '73,200,70,323(again)', '212(todo),211,338,208(again)', '104,230,102,100', '125']
2024-05-22 -> ['322,191,190', '73,200,70,323(again)', '207,79,206,213,198', '347,253(todo),91(todo),217', '105,106']
Expand Down
30 changes: 30 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ public boolean canJump_(int[] nums) {
return lastPos == 0;
}

// V0''
// IDEA : GREEDY (modified by GPT)
public boolean canJump_0(int[] nums) {
// If there is only one element, we are already at the last index
if (nums.length == 1) {
return true;
}

int maxReach = 0; // The maximum index we can currently reach

for (int i = 0; i < nums.length; i++) {
// If we cannot reach this index, return false
if (i > maxReach) {
return false;
}

// Update the maximum index we can reach
maxReach = Math.max(maxReach, i + nums[i]);

// If we can reach or exceed the last index, return true
if (maxReach >= nums.length - 1) {
return true;
}
}

// If we exit the loop without having reached the last index, return false
return false;
}


// V1
// IDEA : Backtracking
// https://leetcode.com/problems/jump-game/editorial/
Expand Down
39 changes: 39 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -1483,4 +1483,43 @@ public int[][] merge(int[][] intervals) {
return tmp.toArray(new int[tmp.size()][]);
}

// LC 55
public boolean canJump(int[] nums) {

if (nums.length == 1){
return true;
}

if (nums.length == 2){
if (nums[0] > 0){
return true;
}
}

if (nums[0] == 0){
return false;
}

int tmp = -1;
int dist = 0;
int startIdx = 0;

for (int i = 0; i < nums.length - 1; i ++){
int cur = nums[i];
if (i == 0 || i - startIdx >= tmp){
startIdx = i;
tmp = cur;
dist += tmp;
} else if (cur > tmp) {
startIdx = i;
tmp = Math.max(tmp, cur);
dist += tmp;
}
}

System.out.println("dist = " + dist);

return dist >= nums.length-1; // dist < nums.length ? false : true;
}

}

0 comments on commit 2382e37

Please sign in to comment.