题目链接
贪心
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int curSum = 0;
int totalSum = 0;
int start = 0;
for(int i = 0; i < gas.length; i++){
// 当前剩油量
curSum += gas[i] - cost[i];
// 总剩油量
totalSum += gas[i] - cost[i];
// 当前剩油量小于零则区间[0,i]不能作为起点
if(curSum < 0){
start = i + 1;
curSum = 0;
}
}
if(totalSum < 0){
return -1;
}
return start;
}
}