【LeetCode每天一题】Gas Station(汽油站)

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.

Note:

  • If there exists a solution, it is guaranteed to be unique.
  • Both input arrays are non-empty and have the same length.
  • Each element in the input arrays is a non-negative integer.

Example 1:

Input: 
gas  = [1,2,3,4,5]
cost = [3,4,5,1,2]

Output: 3

Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

Example 2:

Input: 
gas  = [2,3,4]
cost = [3,4,3]

Output: -1

Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.

思路

  这道题的意思就是让我们找到一个汽油站,从该站开始,循环遍历一圈重新回到起始的汽油站并且要保证汽车的汽油够用。在看到这道题并理解他的例子之后,我的大概思路就是从第一个站台开始找,如果当前站台的汽油存储量小于行驶到下一个汽油站所需的量,我们选择下一个站台为开始站台进行分析。当找到一个汽油站台的存储量大于到达下一个汽油站的消耗量时,我们以当前汽油站为起始汽油站,来进行判断是否能循环一圈。这样一直找到合适的汽油站为止,如果遍历完毕还是未找到,则说明不存在这样的汽油站,返回-1。这种算法的时间复杂度为O(n2),空间复杂度为O(1)。
  上一种思路在写出来之后,我发现测试用例不能全部通过,查看之后发现这种解法相当于暴力破解法,对于数组长度非常大时,会存在超时的问题。因此,看了一下别人的答案。其中有一个人的答案的思路大概是这样的,如果汽油站A到达不了汽油站B,则意味者B也达到不了A,还有一个就是如果所有站的汽油量小于花费量的话,则直接返回-1。这种解法的时间复杂度为O(n),空间复杂度为O(1)。
解决代码

第一种解法
 1 class Solution(object):
 2     def canCompleteCircuit(self, gas, cost):
 3         """
 4         :type gas: List[int]
 5         :type cost: List[int]
 6         :rtype: int
 7         """
 8         if not gas or not cost:
 9             return -1
10         index = 0  
11         while index < len(gas):     # 从第一个开始遍历
12             if gas[index] >= cost[index]:   # 如果当前汽油站的存储量大于达到下一个汽油站的消耗量时。
13                 curgas = gas[index] - cost[index]     # 从以一个站开始进行遍历
14                 tem_index = (index+1) % len(cost)      # 因为涉及到循环遍历,所以用取余来实现。  
15                 while tem_index != index:              #  循环终止条件
16                     if curgas - cost[tem_index] + gas[tem_index] < 0:    # 如果当前存储量小于达到下一个汽油站的消耗量,直接结束循环
17                         break
18                     curgas = curgas - cost[tem_index] + gas[tem_index]   # 记录当前剩余的汽油量
19                     tem_index = (tem_index+1) % len(cost)              # 更新下标
20                 if tem_index == index:              # 循环结束后判断是否循环了一圈。
21                     return index
22             index+= 1
23         return -1              # 说明没有满足的汽油站
第二种解法
 1 class Solution(object):
 2     def canCompleteCircuit(self, gas, cost):
 3         """
 4         :type gas: List[int]
 5         :type cost: List[int]
 6         :rtype: int
 7         """
 8         if not gas or not cost:
 9             return -1
10         gassum, costsum = 0, 0       # 总汽油量和总的消耗量
11         start, tank = 0, 0           # start表示开始的汽油站, tank表示汽车上的汽油量
12         for i in range(len(cost)):
13             gassum += gas[i]          # 累加
14             costsum += cost[i]
15             tank += gas[i] - cost[i]     # tank累加上经过当前汽油站所剩余的汽油量
16             if tank <0:            # 如果tank小于0表示站点A不能到达站站点B,所以更新起始的汽油站,并设置容量为0。
17                 start = i + 1
18                 tank = 0
19         if gassum < costsum:       # 判断总的汽油量和消耗的汽油量之间的关系。
20             return -1
21         return start               # 返回满足条件的起始汽油站
posted @ 2019-05-26 11:43  GoodRnne  阅读(325)  评论(0编辑  收藏  举报