Beat 99% python solutions


  • 0
    B
    class Solution(object):
        def canCompleteCircuit(self, gas, cost):
            """
            :type gas: List[int]
            :type cost: List[int]
            :rtype: int
            """
            if len(gas) == 0 or len(cost) == 0:
                return -1
            
            N = len(gas)
            total = 0
            cur_sum = 0
            start = 0
            for i in range(N):
                diff = gas[i] - cost[i]
                total += diff
                cur_sum += diff
                if cur_sum < 0:
                    start = i + 1
                    cur_sum = 0
           
            if total < 0:
                return -1
            else:
                return start

Log in to reply
 

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