0134.cpp (426B)
1 class Solution { 2 public: 3 int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { 4 int start = 0, total = 0, tank = 0; 5 for (int i = 0; i < gas.size(); i++) { 6 tank = tank + gas[i] - cost[i]; 7 if (tank < 0) { 8 start = i + 1; 9 total += tank; 10 tank = 0; 11 } 12 } 13 return (total + tank >= 0) ? start : -1; 14 } 15 };