难度: Medium
原题连接
内容描述
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(N^2)- 空间复杂度: O(N)******
开始想着有几个要点,
- 如果gas[i] < cost[i],那显然不行,返回-1
- 如果sum(gas) < sum(cost),显然也不行,返回-1
根据要点1得到candidates,对里面的每一个元素进行判断,只要遇到行的立马返回对应idx
但是最后一个case超时了
class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
n = len(gas)
if n == 1 and gas[0] >= cost[0]:
return 0
if sum(gas) < sum(cost):
return -1
def canTravel(idx):
tank = gas[idx]
tmp_idx1 = idx
tmp_idx2 = (idx + 1) % n
while tmp_idx2 != idx and tank - cost[tmp_idx1] >= 0:
tank = tank + gas[tmp_idx2] - cost[tmp_idx1]
tmp_idx1 = (tmp_idx1 + 1) % n
tmp_idx2 = (tmp_idx2 + 1) % n
if tank >= cost[tmp_idx1] and (tmp_idx1 + 1) % n == idx:
return True
return False
candidates = []
for i in range(n):
if gas[i] > cost[i]:
candidates.append(i)
for candidate in candidates:
if canTravel(candidate):
return candidate
return -1
思路 1 - 时间复杂度: O(N)- 空间复杂度: O(1)******
看完评论区,数学好是真的好啊,哈哈哈
依然是两个要点:
- If car starts at A and can not reach B. Any station between A and B can not reach B.(B is the first station that A can not reach.)
- If the total number of gas is bigger than the total number of cost. There must be a solution.
证明如下:
proof of property 1:
Facts:
1. A cannot reach B
2. There are C1,C2, ..., Ck between A and B
3. A can reach C1, C2, ..., Ck
A ---> C1 ---> C2 ---> ... Ck ---> B
Proof by contradiction:
Assume: C1 can reach B
A can reach C1 (by Fact3) & C1 can reach B => A can reach B (Contradict with Fact1 !)
=> assumption is wrong, C1 cannot reach B
Same proof could be applied to C2 ~ Ck
=> any station between A and B that A can reach cannot reach B
proof of property 2:
If the gas is more than the cost in total, there must be some stations we have enough gas to go through them.
Let's say they are green stations. So the other stations are red.
The adjacent stations with same color can be joined together as one.
Then there must be a red station that can be joined into a precedent green station unless there isn't any red station,
because the total gas is more than the total cost.
In other words, all of the stations will join into a green station at last.
Approved
beats 100%
class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
tank, start = 0, 0
for i in range(len(gas)):
tank += gas[i] - cost[i]
if tank < 0:
tank, start = 0, i + 1
return -1 if sum(gas) < sum(cost) else start