Skip to content

Commit

Permalink
add 134 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 6, 2023
1 parent 280e53b commit acea74c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@
20231106: 134
20231103: 53
20231006: quick_sort,286
20231003: 994
Expand Down
13 changes: 9 additions & 4 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
94 changes: 94 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}

}
24 changes: 24 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace2.java
Original file line number Diff line number Diff line change
@@ -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()));

}

}

0 comments on commit acea74c

Please sign in to comment.