My Accepted Solution


  • 0
    A
    class Solution {
    

    public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {

        int n = gas.size();
        int balance=0;
        int index=0;
        int neg=0;
        for(int i=0;i<n;i++)
        {
            balance+=gas[i];
            balance-=cost[i];
            if(balance<0)
            {
                index=i+1;
                neg+= (-balance);
                balance=0;
            }
            
        }
        balance-=neg;
        if(balance>=0)
            return index;
        else
            return -1;
        
        
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.