-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
20231106: 134 | ||
20231103: 53 | ||
20231006: quick_sort,286 | ||
20231003: 994 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
|
||
} | ||
|
||
} |