Leetcode 134. Gas Station
题目
解法:brutal force
从每个gas station出发,check是不是可以成环,复杂度为O(n*n)
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
if sum(gas)<sum(cost):
return -1
for pos in range(len(gas)):
step = 0
curr_pos = pos
remain = 0
while step<=len(gas):
remain = gas[curr_pos]-cost[curr_pos] + remain
if remain < 0:
break