134. Gas Station

PHOTO EMBED

Thu Feb 23 2023 04:15:09 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

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;
    }
};
content_copyCOPY

https://leetcode.com/problems/gas-station/