Problem:
思路:
Solution (C++):
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size(), tank(0), start(0), lack(0);
for (int i = 0; i < n; ++i) {
tank += (gas[i] - cost[i]);
if (tank < 0) {
start = i+1;
lack += tank;
tank = 0;
}
}
return (lack + tank < 0) ? -1: start;
}
性能:
Runtime: 8 ms Memory Usage: 9.1 MB