-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path134. Gas Station.java
53 lines (49 loc) · 1.48 KB
/
134. Gas Station.java
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
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
return GasStation.startingStation(gas, cost);
}
}
public class GasStation {
private static class Station {
int index;
int cost;
int gas;
public Station(int index, int cost, int gas) {
super();
this.index = index;
this.cost = cost;
this.gas = gas;
}
@Override
public String toString() {
return "Station [index=" + index + ", cost=" + cost + ", gas=" + gas + "]";
}
}
public static int startingStation(int[] gases, int[] costs) {
int n = gases.length;
Station[] stations = new Station[n];
for (int i = 0 ; i < n; i++) {
stations[i] = new Station(i, costs[i], gases[i]);
}
int shortage = 0;
int stationpoint = 0;
int totalShortage = 0;
for (Station station : stations) {
shortage += (station.gas - station.cost);
if (shortage < 0) {
shortage = 0;
stationpoint = station.index + 1;
}
totalShortage += (station.gas - station.cost);
}
if (totalShortage >= 0) {
// System.out.println("STP: " + stationpoint);
return stationpoint;
} else {
// System.out.println("STP: " + -1);
return -1;
}
}
}