class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n=gas.size();
int tg=0, cg=0, s=0;
for(int i=0;i<n;i++)
{
tg+=(gas[i]-cost[i]);
cg+=(gas[i]-cost[i]);
if(cg<0)
{
s=i+1;
cg=0;
}
}
if(tg>=0) return s;
else return -1;
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter