File tree 1 file changed +6
-5
lines changed
scripts/algorithms/J/Jump Game IV
1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 147 ms (Top 48.60%) | Memory: 103.8 MB (Top 70.82%)
1
2
/*
2
3
Here we are using map and queue
3
4
map for storing the array elements and where are the other indices of the same element
9
10
i> check the next index (i+1)
10
11
ii> check the previous index(i-1)
11
12
iii> check all the indices of the list whih are present in the map
12
- once these three things have been done we will
13
+ once these three things have been done we will
13
14
remove the element that is arr[i]
14
15
because if we did not remove it we are going to do the same repeated task over and over again
15
16
and this will result in stack overflow
16
- so it is important to remove the indices which have been visited once
17
+ so it is important to remove the indices which have been visited once
17
18
every time we check the queue we incease the answer because viewing a queue means that
18
- we are not at the last index
19
+ we are not at the last index
19
20
20
21
I hope the idea was clear :) you'll understand better when you see the code
21
22
*/
22
23
class Solution {
23
24
public int minJumps (int [] arr ) {
24
25
int n = arr .length ;
25
-
26
+
26
27
if (n <= 1 ) return 0 ;
27
28
Map <Integer , List <Integer >> mp = new HashMap <>();
28
29
for (int i = 0 ;i < arr .length ; i ++) {
@@ -67,4 +68,4 @@ public int minJumps(int[] arr) {
67
68
}
68
69
return ans ;
69
70
}
70
- }
71
+ }
You can’t perform that action at this time.
0 commit comments