mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0134.加油站.md
This commit is contained in:
@ -249,44 +249,74 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
暴力法
|
||||||
```python
|
```python
|
||||||
# 解法1
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||||
n = len(gas)
|
for i in range(len(cost)):
|
||||||
cur_sum = 0
|
rest = gas[i] - cost[i] # 记录剩余油量
|
||||||
min_sum = float('inf')
|
index = (i + 1) % len(cost) # 下一个加油站的索引
|
||||||
|
|
||||||
for i in range(n):
|
while rest > 0 and index != i: # 模拟以i为起点行驶一圈(如果有rest==0,那么答案就不唯一了)
|
||||||
cur_sum += gas[i] - cost[i]
|
rest += gas[index] - cost[index] # 更新剩余油量
|
||||||
min_sum = min(min_sum, cur_sum)
|
index = (index + 1) % len(cost) # 更新下一个加油站的索引
|
||||||
|
|
||||||
if cur_sum < 0: return -1
|
if rest >= 0 and index == i: # 如果以i为起点跑一圈,剩余油量>=0,并且回到起始位置
|
||||||
if min_sum >= 0: return 0
|
return i # 返回起始位置i
|
||||||
|
|
||||||
for j in range(n - 1, 0, -1):
|
return -1 # 所有起始位置都无法环绕一圈,返回-1
|
||||||
min_sum += gas[j] - cost[j]
|
|
||||||
if min_sum >= 0:
|
|
||||||
return j
|
|
||||||
|
|
||||||
return -1
|
|
||||||
```
|
```
|
||||||
|
贪心(版本一)
|
||||||
```python
|
```python
|
||||||
# 解法2
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||||
start = 0
|
curSum = 0 # 当前累计的剩余油量
|
||||||
curSum = 0
|
minFuel = float('inf') # 从起点出发,油箱里的油量最小值
|
||||||
totalSum = 0
|
|
||||||
|
for i in range(len(gas)):
|
||||||
|
rest = gas[i] - cost[i]
|
||||||
|
curSum += rest
|
||||||
|
if curSum < minFuel:
|
||||||
|
minFuel = curSum
|
||||||
|
|
||||||
|
if curSum < 0:
|
||||||
|
return -1 # 情况1:整个行程的总消耗大于总供给,无法完成一圈
|
||||||
|
|
||||||
|
if minFuel >= 0:
|
||||||
|
return 0 # 情况2:从起点出发到任何一个加油站时油箱的剩余油量都不会小于0,可以从起点出发完成一圈
|
||||||
|
|
||||||
|
for i in range(len(gas) - 1, -1, -1):
|
||||||
|
rest = gas[i] - cost[i]
|
||||||
|
minFuel += rest
|
||||||
|
if minFuel >= 0:
|
||||||
|
return i # 情况3:找到一个位置使得从该位置出发油箱的剩余油量不会小于0,返回该位置的索引
|
||||||
|
|
||||||
|
return -1 # 无法完成一圈
|
||||||
|
|
||||||
|
```
|
||||||
|
贪心(版本二)
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||||
|
curSum = 0 # 当前累计的剩余油量
|
||||||
|
totalSum = 0 # 总剩余油量
|
||||||
|
start = 0 # 起始位置
|
||||||
|
|
||||||
for i in range(len(gas)):
|
for i in range(len(gas)):
|
||||||
curSum += gas[i] - cost[i]
|
curSum += gas[i] - cost[i]
|
||||||
totalSum += gas[i] - cost[i]
|
totalSum += gas[i] - cost[i]
|
||||||
if curSum < 0:
|
|
||||||
curSum = 0
|
if curSum < 0: # 当前累计剩余油量curSum小于0
|
||||||
start = i + 1
|
start = i + 1 # 起始位置更新为i+1
|
||||||
if totalSum < 0: return -1
|
curSum = 0 # curSum重新从0开始累计
|
||||||
|
|
||||||
|
if totalSum < 0:
|
||||||
|
return -1 # 总剩余油量totalSum小于0,说明无法环绕一圈
|
||||||
return start
|
return start
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
Reference in New Issue
Block a user