-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN134加油站.py
More file actions
29 lines (24 loc) · 785 Bytes
/
N134加油站.py
File metadata and controls
29 lines (24 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import List
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
i = 0
n = len(gas)
while i < n:
# 过不去或是从下一个节点出发也行
if (gas_gain := gas[i] - cost[i]) < 0:
i += 1
else:
begin = i
j = (i + 1) % n
while gas_gain >= 0:
gas_gain += gas[j] - cost[j]
j = (j + 1) % n
if j == begin and gas_gain >= 0:
return begin
if j <= i:
return -1
else:
i = j
return -1
s = Solution()
print(s.canCompleteCircuit([2, 3, 4], [3, 4, 3]))