-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path1345. Jump Game IV.py
30 lines (27 loc) · 1.03 KB
/
1345. Jump Game IV.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
import collections
from typing import List
class Solution:
def minJumps(self, arr: List[int]) -> int:
positions = collections.defaultdict(set)
n = len(arr)
for i, v in enumerate(arr):
positions[v].add(i)
q = collections.deque([0])
dp = [-1] * n
dp[0] = 0
while q:
from_index = q.popleft()
from_distance = dp[from_index]
if from_index == n - 1:
return dp[from_index]
for next_index in positions[arr[from_index]]:
if dp[next_index] == -1:
dp[next_index] = from_distance + 1
q.append(next_index)
positions[arr[from_index]].clear()
if from_index > 0 and dp[from_index - 1] == -1:
dp[from_index - 1] = from_distance + 1
q.append(from_index - 1)
if from_index + 1 < n and dp[from_index + 1] == -1:
dp[from_index + 1] = from_distance + 1
q.append(from_index + 1)