描述
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, otherwise return -1.
Note:
The solution is guaranteed to be unique.
分析
这道题等价于,对每个加油站,油的存量和到下一个加油站的消耗之差diff[i] = gas[i] -cost[i]
这样一个循环数组,找到一个下标x
,使得从x
开始diff[i]
的累加和永远非负。想到一个O(n^2)
时间的算法很容易,但我们应该找到更好的。
首先,有一个很重要的结论,如果 $\sum_{i=0}^{N-1} gas[i] - cost[i] >= 0 $,则肯定有解。如果小于0,则一定无解。这个结论如何证明需要仔细想一想。然后,如何找到这个下标呢?这个问题其实满足贪心法的最优子结构:
- 从第0个加油站开始,寻找第一个使得
sum(diff[i]) < 0
的下标,如果找不到,则说明,第一个加油站就满足条件。 - 找到这样的下标
i
,则说明0..i
之间的加油站肯定不满足条件。可能的解只会在i+1 ... N-1
之间。 - 将
sum
清零,从i+1
开始继续搜索diff累加和为负的下标j
,找到则说明i+1 ... j
之间的加油站都不符合条件。以此类推。 - 最后判断全体的diff和是否非负,如果是,则按照之前的结论,肯定有解,这个解就是我们上面搜索过程中最后一个累加起始位置。
时间复杂度O(n)
,空间复杂度O(1)
。
代码
Python
1 | class Solution(object): |