-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (24 loc) · 801 Bytes
/
Solution.java
File metadata and controls
28 lines (24 loc) · 801 Bytes
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
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost == null || gas.length != cost.length)
return -1;
int[] gasLeft = new int[gas.length];
for (int i = 0; i < gas.length;) {
int j = 0;
if (gas[i] >= cost[i]) {
int remainGas = 0;
for (; j < gas.length && remainGas >= 0; j++) {
int idx = (i + j) % gas.length;
remainGas += gas[idx] - cost[idx];
}
if (j == gas.length && remainGas >= 0)
return i;
}
if (j > 0)
i += j;
else
i++;
}
return -1;
}
}