-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpGameIV.py
More file actions
38 lines (36 loc) · 1.14 KB
/
jumpGameIV.py
File metadata and controls
38 lines (36 loc) · 1.14 KB
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/jump-game-iv/
# Author: Miao Zhang
# Date: 2021-04-25
class Solution:
def minJumps(self, arr: List[int]) -> int:
n = len(arr)
m = collections.defaultdict(list)
for i in range(n):
m[arr[i]].append(i)
q = collections.deque()
q.append(0)
seen = [0 for _ in range(n)]
seen[0] = 1
steps = 0
while q:
qlen = len(q)
for _ in range(qlen):
cur = q.popleft()
if cur == n - 1: return steps
if cur - 1 >= 0 and not seen[cur - 1]:
q.append(cur - 1)
seen[cur - 1] = 1
if cur + 1 < n and not seen[cur + 1]:
q.append(cur + 1)
seen[cur + 1] = 1
if arr[cur] not in m:
continue
for nxt in m[arr[cur]]:
if not seen[nxt]:
seen[nxt] = 1
q.append(nxt)
m.pop(arr[cur])
steps += 1
return -1