Skip to content

Commit fbf332d

Browse files
134. Gas Station (java)
1 parent a3527c7 commit fbf332d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int canCompleteCircuit(int[] gas, int[] cost) {
3+
if(gas.length!=cost.length) return -1;
4+
int sum=0,total=0,index=0;
5+
for(int i=0;i<gas.length;i++){
6+
int sy = gas[i] - cost[i];
7+
sum+= sy;
8+
total+= sy;
9+
if(sum<0){
10+
index=i+1;
11+
sum=0;
12+
}
13+
}
14+
if(total<0) return -1;
15+
return index;
16+
}
17+
}

0 commit comments

Comments
 (0)