-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGasStation_134.java
36 lines (33 loc) · 1.14 KB
/
GasStation_134.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
/**
* - if we want to drive from station_i to station_j,
* we should satisfy sum(gas[i]) > sum(cost[i]) for each intermediate station
* - if we can not drive from station_i to station_j
* any station between i & j can not reach station_j
* - O(n) time & O(1) space
* - Other Comments
* If the total number of gas is bigger than the total number of cost. There must be a solution
*/
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int length = gas.length;
//if (length == 0) return -1;
//if (length == 1) return 0; // when length == 1 should return 0
int station = 0;
while(station < length) {
int sumg = 0, sumc = 0;
int i=station;
for(; i<station+length; i++) {
sumg += gas[i%length];
sumc += cost[i%length];
if (sumg < sumc) {
station = i+1;
break;
}
}
// length = 1 also satisfy
if (i == station+length)
return station;
}
return -1;
}
}