2022/01/20 - 134. Gas Station
134. Gas Station - Medium
Description¶
134. Gas Station
There are n
gas stations along a circular route, where the amount of gas at the ith
station is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from the ith
station to its next (i + 1)th
station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas
and cost
, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1
. If there exists a solution, it is guaranteed to be unique
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.
其实就是一道找规律的数组题,首先能想出来的办法就是暴力遍历,将每一个位置都当成是第一个station
,看是否可以转一圈回到这个地方。但是算法的时间复杂度是O(n)
,所以TLE了。那么考虑一下这种方法有没有优化的余地呢? 因为这个暴力穷举的过程中,变化的量只有起点
和当前油箱的油量
。这两种状态的组合一定有不下n^2
种,所以没有任何优化空间。
Solution¶
图像¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
贪心算法¶
其实是和图像解法差不多的,就是思路不太一样。
假设从i
开始为起点,到j
时,总油量小于0了,说明i
无法到j
,这也侧面说明了i
和j
之间的点都无法走到j
,例如k
为i
和j
之间的点,i
到k
的时候总油量一定是大于等于0的,如果从k
点开始,起始油量一定是0,那么想一下,从i
到k
的时候油量大于零都无法到达j
,那么k
开始油量为0,就更不可能到达j
了。
所以当遍历到一个点时,当前的邮箱总量小于0,那就就让起始点start
为i+1
,然后tank
(总油量)重新设置为0。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|