Share my Python Code, 6 lines, 40ms, O(n) time, O(1) space


  • 0
    S
    class Solution(object):
        def canCompleteCircuit(self, gas, cost):
            start, n, s = 0, len(gas), 0
            for i in range(2 * n):
                s += gas[i % n] - cost[i % n]
                if s < 0: s, start = 0, i + 1
                if i >= start + n: return start
            return -1

Log in to reply
 

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