On time and constant space:Two pointers with python 32ms beats 81.93%


  • 1
    E
    def canCompleteCircuit(self, gas, cost):
        i,j,gasleft,f,idx=0,len(gas),0,True,0
        while i<j:
            p=i if f else j
            gasleft+=gas[p]-cost[p]
            if gasleft>=0 and not f:idx=p;f=True
            elif gasleft<0:f=False
            if f:i+=1
            else:j-=1
        if gasleft>=0:return idx
        else:return -1
    

Log in to reply
 

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