Skip to content

Instantly share code, notes, and snippets.

@Robogeek95
Created September 30, 2020 21:03
Show Gist options
  • Save Robogeek95/3f9aad3b686011a5ef93a15a2c0ca3b1 to your computer and use it in GitHub Desktop.
Save Robogeek95/3f9aad3b686011a5ef93a15a2c0ca3b1 to your computer and use it in GitHub Desktop.
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