Skip to content

Commit a0cd0fc

Browse files
Sync LeetCode submission Runtime - 101 ms (49.50%), Memory - 83.4 MB (40.13%)
1 parent 80880e3 commit a0cd0fc

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>Given an array of&nbsp;integers <code>arr</code>, you are initially positioned at the first index of the array.</p>
2+
3+
<p>In one step you can jump from index <code>i</code> to index:</p>
4+
5+
<ul>
6+
<li><code>i + 1</code> where:&nbsp;<code>i + 1 &lt; arr.length</code>.</li>
7+
<li><code>i - 1</code> where:&nbsp;<code>i - 1 &gt;= 0</code>.</li>
8+
<li><code>j</code> where: <code>arr[i] == arr[j]</code> and <code>i != j</code>.</li>
9+
</ul>
10+
11+
<p>Return <em>the minimum number of steps</em> to reach the <strong>last index</strong> of the array.</p>
12+
13+
<p>Notice that you can not jump outside of the array at any time.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> arr = [100,-23,-23,404,100,23,23,23,3,404]
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> You need three jumps from index 0 --&gt; 4 --&gt; 3 --&gt; 9. Note that index 9 is the last index of the array.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> arr = [7]
28+
<strong>Output:</strong> 0
29+
<strong>Explanation:</strong> Start index is the last index. You do not need to jump.
30+
</pre>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> arr = [7,6,9,6,9,6,9,7]
36+
<strong>Output:</strong> 1
37+
<strong>Explanation:</strong> You can jump directly from index 0 to index 7 which is last index of the array.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li>
45+
<li><code>-10<sup>8</sup> &lt;= arr[i] &lt;= 10<sup>8</sup></code></li>
46+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int minJumps(vector<int>& arr) {
4+
int n = arr.size();
5+
if (n == 0) return 0;
6+
unordered_map<int, vector<int>> nums;
7+
unordered_map<int, bool> numsVis;
8+
for (int i=0; i<n; i++) nums[arr[i]].push_back(i);
9+
vector<int> dist(n, 1e9);
10+
dist[0] = 0;
11+
queue<int> q;
12+
q.push(0);
13+
while (!q.empty()) {
14+
int node = q.front();
15+
q.pop();
16+
if (node < n-1 && dist[node+1] == 1e9) {
17+
dist[node+1] = dist[node] + 1;
18+
q.push(node+1);
19+
}
20+
if (node > 0 && dist[node-1] == 1e9) {
21+
dist[node-1] = dist[node] + 1;
22+
q.push(node-1);
23+
}
24+
if (numsVis[arr[node]]) continue;
25+
for (int neigh : nums[arr[node]]) {
26+
if (dist[neigh] == 1e9) {
27+
dist[neigh] = dist[node] + 1;
28+
q.push(neigh);
29+
}
30+
}
31+
numsVis[arr[node]] = true;
32+
}
33+
return dist[n-1];
34+
}
35+
};

0 commit comments

Comments
 (0)