-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1345. Jump Game IV.cpp
64 lines (57 loc) · 1.74 KB
/
1345. 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ____ _ _ _ _ __ __ _ _ _
// | _ \(_)___| |__ __ _| |__ | |__ | \/ | __ _| | |__ ___ | |_ _ __ __ _
// | |_) | / __| '_ \ / _` | '_ \| '_ \ | |\/| |/ _` | | '_ \ / _ \| __| '__/ _` |
// | _ <| \__ \ | | | (_| | |_) | | | | | | | | (_| | | | | | (_) | |_| | | (_| |
// |_| \_\_|___/_| |_|\__,_|_.__/|_| |_| |_| |_|\__,_|_|_| |_|\___/ \__|_| \__,_|
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
/**
* return the steps
* have a unordered map - vector
* have a Queue and do level order travel-ish on it (while-for)
* the next which counts the stepn, its importannt to pass by reference and the clear() it
* Visited array to only add unvisted indices to the queue
*/
int minJumps(vector<int> &arr)
{
int n = arr.size();
unordered_map<int, vector<int>> umap;
for (int i = 0; i < n; i++)
umap[arr[i]].push_back(i);
vector<bool> visited(n, false);
// visited[0] = true;
queue<int> q;
q.push(0);
int steps = 0;
while (q.empty() == false)
{
for (int i = q.size() - 1; i >= 0; i--)
{
int pos = q.front();
q.pop();
if (pos == n - 1)
return steps;
// you have a fookin next_el array
vector<int> &next_el = umap[arr[pos]];
next_el.push_back(pos - 1);
next_el.push_back(pos + 1);
for (int j : next_el)
{
// here what we do is selectively add things to look in the queue
if (j >= 0 && j < n && visited[j] == false)
{
q.push(j);
visited[j] = true;
}
}
next_el.clear();
}
steps++;
}
return 0;
// level order traversal
}
};