forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump-game-iv.cpp
33 lines (32 loc) · 1.03 KB
/
jump-game-iv.cpp
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
// Time: O(n)
// Space: O(n)
class Solution {
public:
int minJumps(vector<int>& arr) {
unordered_map<int, vector<int>> groups;
for (int i = 0; i < arr.size(); ++i) {
groups[arr[i]].emplace_back(i);
}
int result = 0;
queue<pair<int, int>> q({{0, 0}});
unordered_set<int> lookup = {0};
while (!q.empty()) {
const auto [pos, step] = q.front(); q.pop();
if (pos == arr.size() - 1) {
result = step;
break;
}
unordered_set<int> neighbors(groups[arr[pos]].cbegin(),
groups[arr[pos]].cend());
groups[arr[pos]].clear();
neighbors.emplace(pos - 1), neighbors.emplace(pos + 1);
for (const auto& p : neighbors) {
if (0 <= p && p < arr.size() && !lookup.count(p)) {
lookup.emplace(p);
q.emplace(p, step + 1);
}
}
}
return result;
}
};