Can someone tell me why it is the right answer


  • 0
    Y
    class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        """
        :type gas: List[int]
        :type cost: List[int]
        :rtype: int
        """
        sth=len(gas)
        s=[0]*sth
        for i in range(len(gas)):
            s[i]=gas[i]-cost[i]
        left=0
        ans=s[0]
        for i in range(sth):
            if ans<0:
                ans=0
                left=i
            ans+=s[i]
        return left if sum(s)>=0 else -1

Log in to reply
 

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