Created
September 30, 2020 21:03
-
-
Save Robogeek95/3f9aad3b686011a5ef93a15a2c0ca3b1 to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/gas-station/ | my solution in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { | |
int st=0, left=0, sum=0; // left-gas left in the tank | |
for (int i=0; i<gas.size(); i++) { | |
left+=gas[i]-cost[i]; | |
sum+=gas[i]-cost[i]; | |
if (sum<0) { | |
sum=0; | |
st=i+1; | |
} | |
} | |
return left<0 ? -1:st; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment