-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGasStation.java
More file actions
23 lines (21 loc) · 537 Bytes
/
Copy pathGasStation.java
File metadata and controls
23 lines (21 loc) · 537 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int sum = 0;
int n = gas.length;
int pSum = 0;
int cand = -1;
for(int i = 0; i < n; i++) {
int t = gas[i] - cost[i];
sum += t;
pSum += t;
if(pSum < 0) {
cand = -1;
pSum = 0;
}else{
if(cand == -1) cand = i;
}
}
if (sum < 0) return -1;
return cand;
}
}