Gas Station
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int tank = 0;
int total = 0;
int n = gas.size();
int index = -1;
for(int i=0;i<n;i++)
{
total += (gas[i]-cost[i]);
tank += (gas[i]-cost[i]);
if(tank<0)
{
tank = 0;
index = i;
}
}
return (total>=0?index+1:-1);
}
浙公网安备 33010602011771号