My simple python answer O(N), O(1)


  • -3
    S
    class Solution:
        # @param gas, a list of integers
        # @param cost, a list of integers
        # @return an integer
        def canCompleteCircuit(self, gas, cost):
            if sum(gas) < sum(cost):
                return -1
            n = len(gas)
            res = 0
            s = 0
            for i in range(n):
                s +=  gas[i] - cost[i]
                if s < 0:
                    s = 0
                    res = (i + 1) % n
            return res

Log in to reply
 

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