Friday, September 12, 2014

Leetcode: Gas Station @Python

class Solution:
    # @param gas, a list of integers
    # @param cost, a list of integers
    # @return an integer
    def canCompleteCircuit(self, gas, cost):
        n=len(gas)
        Sum=total=0
        k=0
        for i in range(n):
            Sum+=gas[i]-cost[i]
            total+=gas[i]-cost[i]
            if Sum<0:
                k=i+1
                Sum=0
        return k if total>=0 else -1

No comments :

Post a Comment