generated from projeto-de-algoritmos-2024/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path818_race_car.py
30 lines (24 loc) · 1.09 KB
/
818_race_car.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
class Solution:
def racecar(self, target: int) -> int:
from collections import deque
queue = deque([(0, 1, 0)]) # (position, speed, steps)
visited = set((0, 1))
while queue:
position, speed, steps = queue.popleft()
# If we reach the target, return the number of steps
if position == target:
return steps
# Accelerate instruction
next_position, next_speed = position + speed, speed * 2
if (
next_position,
next_speed,
) not in visited and 0 <= next_position < 2 * target:
visited.add((next_position, next_speed))
queue.append((next_position, next_speed, steps + 1))
# Reverse instruction
next_position, next_speed = position, -1 if speed > 0 else 1
if (next_position, next_speed) not in visited:
visited.add((next_position, next_speed))
queue.append((next_position, next_speed, steps + 1))
return -1 # This line should never be reached