public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int gasSum = 0;
int costSum = 0;
int start = 0;
int gasLeft = 0;
for (int i = 0; i < gas.length; i++) {
gasSum += gas[i];
costSum += cost[i];
if (gasLeft < 0) {
start = i;
gasLeft = 0;
}
gasLeft = gasLeft + (gas[i] - cost[i]);
}
if (gasSum < costSum) {
start = -1;
}
return start;
}
}
LeetCode Gas Station
最新推荐文章于 2023-05-29 15:50:15 发布