From 2382e3744578b27ab5496be025ae88d5b9b3b900 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 24 May 2024 09:14:00 +0800 Subject: [PATCH] update java 55, progress --- README.md | 2 +- data/progress.txt | 1 + data/to_review.txt | 16 ++++---- .../java/LeetCodeJava/Greedy/JumpGame.java | 30 ++++++++++++++ .../src/main/java/dev/workspace3.java | 39 +++++++++++++++++++ 5 files changed, 80 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a962ca97..a99dc0aa 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 39e0808f..7fe749fe 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240524: 55 20240523: 56 20240522: 62,572,57 20240521: 322,191,190 diff --git a/data/to_review.txt b/data/to_review.txt index 38b24c7b..e00d4905 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,3 +1,4 @@ +2024-07-18 -> ['55'] 2024-07-17 -> ['56'] 2024-07-16 -> ['62,572,57'] 2024-07-15 -> ['322,191,190'] @@ -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'] @@ -24,7 +26,7 @@ 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'] @@ -32,19 +34,19 @@ 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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java b/leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java index 01ebc0b5..ab7b796a 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Greedy/JumpGame.java @@ -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/ diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 8dfc0677..a19b9678 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -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; + } + }