-
Notifications
You must be signed in to change notification settings - Fork 0
/
134_medium_[greedy]_gas_station.py
50 lines (37 loc) · 1.34 KB
/
134_medium_[greedy]_gas_station.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import List
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
index_list = []
gas_and_cost = zip(gas, cost)
prev_gas_cost_diff = -1
for i, (gas_val, cost_val) in enumerate(gas_and_cost):
current_gas_cost_diff = gas_val - cost_val
if current_gas_cost_diff >= 0 and prev_gas_cost_diff < 0:
index_list.append(i)
prev_gas_cost_diff = current_gas_cost_diff
def dfs(current_index: int, start_index: int, gas_cost_diff_state: int) -> int:
if gas_cost_diff_state < 0:
return -1
if current_index != start_index and current_index%len(gas) == start_index:
return start_index
current_gas_cost_diff = gas[current_index%len(gas)] - cost[current_index%len(gas)]
return dfs(
current_index+1,
start_index,
gas_cost_diff_state+current_gas_cost_diff
)
for i in index_list:
result = dfs(i, i, 0)
if result >= 0:
return result
return -1
if __name__ == '__main__':
s = Solution()
assert s.canCompleteCircuit(
[1,2,3,4,5],
[3,4,5,1,2]
) == 3
assert s.canCompleteCircuit(
[2,3,4],
[3,4,3]
) == -1