Repository navigation
Expand file tree
/
Copy pathP134_Gas_Station.java
More file actions
61 lines (57 loc) · 1.7 KB
/
Copy pathP134_Gas_Station.java
File metadata and controls
61 lines (57 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package solution.medium;
/**
* @author Yi-Lo
* 2021/1/21 19:26
* @version 1.0
* @topics Greedy
* @urllink https://leetcode-cn.com/problems/gas-station/
*/
public class P134_Gas_Station {
public static int canCompleteCircuit_s1(int[] gas, int[] cost) {
for (int i = 0; i < gas.length; i++) {
if (gas[i] < cost[i]) continue;
int tank = gas[i] - cost[i];
for (int j = 1; j <= gas.length; j++) {
int station = (i + j) % gas.length;
tank += gas[station];
if (i == station) return i;
if (tank >= cost[station]) {
tank -= cost[station];
} else {
break;
}
}
}
return -1;
}
public static int canCompleteCircuit_s2(int[] gas, int[] cost) {
for (int i = 0; i < gas.length; i++) {
gas[i] -= cost[i];
}
boolean find = true;
int tank = 0, next = 0;
while (next < gas.length) {
for (int j = 0; j < gas.length; j++) {
tank += gas[(next + j) % gas.length];
if (tank < 0) {
find = false;
next += j + 1;
break;
}
}
if (find) {
return next;
} else {
tank = 0;
find = true;
}
}
return -1;
}
public static void main(String[] args) {
int[] gas = {1, 2, 3, 4, 5};
int[] cost = {3, 4, 5, 1, 2};
System.out.println(canCompleteCircuit_s1(gas, cost));
System.out.println(canCompleteCircuit_s2(gas, cost));
}
}