mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -239,6 +239,30 @@ class Solution {
|
|||||||
|
|
||||||
### Python
|
### Python
|
||||||
```python
|
```python
|
||||||
|
# 解法1
|
||||||
|
class Solution:
|
||||||
|
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||||
|
n = len(gas)
|
||||||
|
cur_sum = 0
|
||||||
|
min_sum = float('inf')
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
cur_sum += gas[i] - cost[i]
|
||||||
|
min_sum = min(min_sum, cur_sum)
|
||||||
|
|
||||||
|
if cur_sum < 0: return -1
|
||||||
|
if min_sum >= 0: return 0
|
||||||
|
|
||||||
|
for j in range(n - 1, 0, -1):
|
||||||
|
min_sum += gas[j] - cost[j]
|
||||||
|
if min_sum >= 0:
|
||||||
|
return j
|
||||||
|
|
||||||
|
return -1
|
||||||
|
```
|
||||||
|
|
||||||
|
```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
|
start = 0
|
||||||
|
Reference in New Issue
Block a user