leetcode:134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
假设从第i个Station出发,它能够重新回到出发点,那么需要满足以下条件:
所以从第1个车站后开始依次累加加油量与消耗量的差值,当第一次累加值为负时 (比如第i个车站), 说明前i-1个车站都不能作为始发车站:
然后从第i+1个车站开始重新累加。直到从某个车站开始,累加值始终为正。最后会出现两种 情况:
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas.length == 0)
return -1;
if (gas.length != cost.length)
return -1;
int index=0;
int sum=0;
for (int i=0; i < gas.length; i++) {
sum += gas[i]-cost[i];
if (sum < 0) {
index=i+1;
sum=0;
}
}
if (index == gas.length)
return -1;
for (int i=0; i < index; i++) {
sum += gas[i] - cost[i];
if (sum < 0)
return -1;
}
return index;
}
}