-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGasStation.java
55 lines (55 loc) · 1.52 KB
/
GasStation.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
54
55
/**
* Created by JiahengYu on 10/06/15.
*/
public class GasStation {
/**
* time limit exceeds
*/
// public int canCompleteCircuit(int[] gas, int[] cost) {
// int diff[]=new int[gas.length];
// for (int i=0;i<diff.length;i++){
// diff[i]=gas[i]-cost[i];
// }
// for (int i=0;i<diff.length;i++){
// if (diff[i]<=0)
// continue;
// else {
// boolean circle=true;
// int currentGas=diff[i];
// for (int j=i+1;j<diff.length+i;j++){
// currentGas+=diff[j%diff.length];
// if (currentGas<0) {
// circle=false;
// break;
// }
// }
// if (circle)
// return i;
// }
// }
// return -1;
// }
public int canCompleteCircuit(int[] gas, int[] cost) {
int diff[]=new int[gas.length];
for (int i=0;i<diff.length;i++){
diff[i]=gas[i]-cost[i];
}
int currentSum=0;
int currentBeg=0;
for (int i=0;i<diff.length;i++){
currentSum+=diff[i];
if(currentSum<0) {
currentBeg = i+1;
currentSum = 0;
}
}
int Sum=0;
for (int i=currentBeg;i<diff.length+currentBeg;i++){
Sum+=diff[i%diff.length];
if (Sum<0){
return -1;
}
}
return currentBeg;
}
}