From acea74ca6007980e0b7effccb19cb0777d1c6679 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 6 Nov 2023 18:53:23 +0800 Subject: [PATCH] add 134 java, update progress --- README.md | 2 +- data/progress.txt | 1 + data/to_review.txt | 13 ++- .../java/LeetCodeJava/Greedy/GasStation.java | 94 +++++++++++++++++++ .../src/main/java/dev/workSpace2.java | 24 +++++ 5 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java create mode 100644 leetcode_java/src/main/java/dev/workSpace2.java diff --git a/README.md b/README.md index 900265dd..72860e84 100644 --- a/README.md +++ b/README.md @@ -1053,7 +1053,7 @@ 055| [Jump Game](https://leetcode.com/problems/jump-game/) |[Python](./leetcode_python/Greedy/jump-game.py) | _O(n)_ | _O(1)_| Medium|Curated Top 75, good trick, Greedy, DP, `amazon`| AGAIN******** (5) 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) | _O(n)_ | _O(1)_ | Medium|trick, greedy,`amazon`| AGAIN****** (5) +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) 135| [Candy](https://leetcode.com/problems/candy/)| [Python](./leetcode_python/Greedy/candy.py) | _O(n)_ | _O(1)_ | Hard|LC 123, greedy, array, `amazon`| AGAIN (not start) 218| [The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/description/)| [Python](./leetcode_python/Greedy/the_skyline_problem.py) | _O(n)_ | _O(1)_ | Hard|brute force, swipe line, Priority Queue, Union Find, Divide-and-Conquer, apple, microsoft| AGAIN (not start) 330| [Patching Array](https://leetcode.com/problems/patching-array/)| [Python](./leetcode_python/Greedy/patching-array.py) | _O(n)_ | _O(1)_ | Hard| array, greedy,`amazon`| AGAIN (not start) diff --git a/data/progress.txt b/data/progress.txt index fbccdb11..90cc94b0 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20231106: 134 20231103: 53 20231006: quick_sort,286 20231003: 994 diff --git a/data/to_review.txt b/data/to_review.txt index 133fe212..ed0fbd60 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,13 +1,18 @@ +2023-12-31 -> ['134'] 2023-12-28 -> ['53'] +2023-12-10 -> ['134'] 2023-12-07 -> ['53'] 2023-11-30 -> ['quick_sort,286'] -2023-11-27 -> ['994'] +2023-11-27 -> ['134', '994'] 2023-11-25 -> ['417'] 2023-11-24 -> ['53', '133,695'] +2023-11-19 -> ['134'] 2023-11-16 -> ['53'] -2023-11-11 -> ['53'] -2023-11-09 -> ['quick_sort,286'] -2023-11-08 -> ['53'] +2023-11-14 -> ['134'] +2023-11-11 -> ['134', '53'] +2023-11-09 -> ['134', 'quick_sort,286'] +2023-11-08 -> ['134', '53'] +2023-11-07 -> ['134'] 2023-11-06 -> ['53', '994'] 2023-11-05 -> ['53'] 2023-11-04 -> ['53', '417'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java b/leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java new file mode 100644 index 00000000..f66b290f --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java @@ -0,0 +1,94 @@ +package LeetCodeJava.Greedy; + +// https://leetcode.com/problems/gas-station/ + +import java.util.ArrayList; +import java.util.List; + +public class GasStation { + + // VO + // IDEA: GREEDY + // https://www.bilibili.com/video/BV1jA411r7WX/?share_source=copy_web&vd_source=49348a1975630e1327042905e52f488a + /** + * total_gain = gain1 + gain2 + .... + * + * if total_gain > 0, then gain1 + gain2 + ... >= 0 + * + * then gain1 >= - (gain2 + gain3 + ...) + * + * -> means car start from such idx (gain1) is ENOUGH to visit all stops + * + */ + public int canCompleteCircuit(int[] gas, int[] cost) { + + if (gas == null || cost == null || gas.length == 0 || cost.length == 0){ + return 0; + } + + int remain = 0; + int total = 0; + int start = 0; + + for (int i = 0; i < gas.length; i++){ + remain += (gas[i] - cost[i]); + // keep maintaining total, since NEED to know whether this start point can finish visit all stops + // example : we start again on idx = 3, still need to know if car can go idx = 3, 4 ... N .. 0 and back to 3 + total += (gas[i] - cost[i]); + + if (remain < 0){ + remain = 0; + start = i+1; + } + } + + return total < 0 ? -1 : start; + } + +// public int canCompleteCircuit(int[] gas, int[] cost) { +// +// if (gas == null || cost == null || gas.length == 0 || cost.length == 0){ +// return 0; +// } +// +// List diffList = new ArrayList(); +// +// diffList.add(gas[1] - cost[cost.length-1]); +// +// for (int i = 1; i < gas.length; i++){ +// int diff = gas[i] - cost[i-1]; +// diffList.add(diff); +// } +// +// // reset start point if current sum < diff +// for (int i = 0; i < diffList.size(); i++){ +// if (diffList.get(i) > 0){ +// return i; +// } +// } +// return -1; +// } + + // V1 + // IDEA : ONE PASS + // https://leetcode.com/problems/gas-station/editorial/ + public int canCompleteCircuit_2(int[] gas, int[] cost) { + int currGain = 0, totalGain = 0, answer = 0; + + for (int i = 0; i < gas.length; ++i) { + // gain[i] = gas[i] - cost[i] + totalGain += gas[i] - cost[i]; + currGain += gas[i] - cost[i]; + + // If we meet a "valley", start over from the next station + // with 0 initial gas. + if (currGain < 0) { + answer = i + 1; + currGain = 0; + } + } + + return totalGain >= 0 ? answer : -1; + } + +} diff --git a/leetcode_java/src/main/java/dev/workSpace2.java b/leetcode_java/src/main/java/dev/workSpace2.java new file mode 100644 index 00000000..8373bd9b --- /dev/null +++ b/leetcode_java/src/main/java/dev/workSpace2.java @@ -0,0 +1,24 @@ +package dev; + +public class workSpace2 { + + public static void main(String[] args) { + +// Integer[] my_list = new Integer[0]; +// for (int i = 0; i < my_list.length; i++){ +// System.out.println(my_list[i]); +// +// Integer.parseInt("1"); +// +// } + + String x = "abcd"; + System.out.println(x.substring(1,3)); + StringBuilder sb = new StringBuilder(x.substring(1,3)); + System.out.println(sb.reverse()); + + System.out.println(x.substring(x.length()-1, x.length())); + + } + +}