lintcode_187.加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i]
,并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]
。
你有一辆油箱容量无限大的汽车,现在要从某一个加油站出发绕环路一周,一开始油箱为空。
求可环绕环路一周时出发的加油站的编号,若不存在环绕一周的方案,则返回-1
。
注意事项
数据保证答案唯一。
样例
现在有4个加油站,汽油量gas[i]=[1, 1, 3, 1]
,环路旅行时消耗的汽油量cost[i]=[2, 2, 1, 1]
。则出发的加油站的编号为2。
思路:
要使能环形一圈,则每次到达车站的油余量大于等于0
class Solution: """ @param: gas: An array of integers @param: cost: An array of integers @return: An integer """ def canCompleteCircuit(self, gas, cost): # write your code here num = len(gas) gas *= 2
cost *= 2
if sum(gas) < sum(cost): return -1 for i in range(num): ans = gas[i] tag = True for j in range(num): if ans < cost[(i+j)]: tag = False break else: ans = ans - cost[(i+j)] + gas[(i+j)] if tag == False: continue if ans >= 0: return i
报超时
九章:
class Solution: # @param gas, a list of integers # @param cost, a list of integers # @return an integer def canCompleteCircuit(self, gas, cost): # write your code here n = len(gas) diff = [] for i in xrange(n): diff.append(gas[i]-cost[i]) for i in xrange(n): diff.append(gas[i]-cost[i]) if n==1: if diff[0]>=0: return 0 else: return -1 st = 0 now = 1 tot = diff[0] while st<n: while tot<0: st = now now += 1 tot = diff[st] if st>n: return -1 while now!=st+n and tot>=0: tot += diff[now] now += 1 if now==st+n and tot>=0: return st return -1
区别在于,如果发现从i出发,第j节点无法到达,则从i+j开始作为出发点,跳过中间部分
改写第一次的程序
class Solution: """ @param: gas: An array of integers @param: cost: An array of integers @return: An integer """ def canCompleteCircuit(self, gas, cost): # write your code here num = len(gas) diff = [] for i in range(num): diff.append(gas[i]-cost[i]) diff *= 2 if sum(gas) < sum(cost): return -1 i = 0 while i < num: tag = True if diff[i] < 0: i += 1 continue ans = diff[i] for j in range(1,num): if ans < 0 : tag = False break else: ans += diff[i+j] if tag == False: i = i + j continue if ans >= 0: return i else: i += 1 return -1