leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0134.cpp (426B)


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