class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int start = 0;
int leftGas = 0;
for(int i = 0; i < 2 * gas.size(); i++) {
leftGas += gas[i % gas.size()] - cost[i % gas.size()];
if(leftGas < 0) {
start = i + 1;
leftGas = 0;
}
}
if(start >= gas.size())
return -1;
else
return start;
}
};
LeetCode(134) Gas Station
最新推荐文章于 2022-07-16 10:08:25 发布