-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpGameIV.cpp
More file actions
35 lines (34 loc) · 1.03 KB
/
jumpGameIV.cpp
File metadata and controls
35 lines (34 loc) · 1.03 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
// Source: https://leetcode.com/problems/jump-game-iv/
// Author: Miao Zhang
// Date: 2021-04-25
class Solution {
public:
int minJumps(vector<int>& arr) {
int n = arr.size();
unordered_map<int, vector<int>> m;
for (int i = 0; i < n; i++)
m[arr[i]].push_back(i);
queue<int> q({0});
vector<int> seen(n);
seen[0] = 1;
int steps = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
int cur = q.front();
q.pop();
if (cur == n - 1) return steps;
if (cur - 1 >= 0 && !seen[cur - 1]++) q.push(cur - 1);
if (cur + 1 < n && !seen[cur + 1]++) q.push(cur + 1);
auto it = m.find(arr[cur]);
if (it == m.end()) continue;
for (int nxt: it->second) {
if (!seen[nxt]++) q.push(nxt);
}
m.erase(it);
}
steps++;
}
return -1;
}
};