Skip to content

Commit 0ab41ac

Browse files
committed
Day80
1 parent ceecd45 commit 0ab41ac

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

Day80/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number}
4+
*/
5+
var minJumps = function(arr) {
6+
7+
let map = {};
8+
9+
if(arr.length ==1)return 0
10+
for(let i =0; i < arr.length; i++){
11+
let key = arr[i];
12+
if(!(key in map))map[key]=[];
13+
map[key].push(i)
14+
}
15+
console.log(map)
16+
17+
/*
18+
19+
{
20+
'3': [ 8 ],
21+
'23': [ 5, 6, 7 ],
22+
'100': [ 0, 4 ],
23+
'404': [ 3, 9 ],
24+
'-23': [ 1, 2 ]
25+
}
26+
27+
*/
28+
29+
let queue = [[0, 0]];
30+
let set = new Set();
31+
32+
while(queue.length){
33+
34+
let [index, step] = queue.shift();
35+
//if index is visited
36+
37+
if(index == arr.length-1) return step;
38+
if(index+1 == arr.length-1) return step +1;
39+
if(set.has(index))continue;
40+
41+
set.add(index)
42+
43+
44+
//get all data for thisd given val at this index
45+
let getData = map[arr[index]];
46+
47+
//iterate backward
48+
for(let i= getData.length-1; i>=0; i--){
49+
50+
if(!set.has(getData[i])){
51+
queue.push([getData[i], step+1])
52+
}
53+
}
54+
55+
56+
const next = index +1;
57+
const prev = index -1;
58+
59+
if(!set.has(next) && next <= arr.length-1)queue.push([next, step+1]);
60+
if(!set.has(prev) && prev >=0)queue.push([prev, step+1]);
61+
map[arr[index]] = [];
62+
63+
}
64+
65+
66+
return -1;
67+
68+
69+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
178178

179179
|Day 78| [498. Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [javascript]()|[:memo:](https://leetcode.com/problems/diagonal-traverse/)|Medium|
180180

181-
|Day 79| [91. Decode Ways](https://leetcode.com/problems/decode-ways/) | [javascript]()|[:memo:](https://leetcode.com/problems/decode-ways/)|Medium|
181+
|Day 79| [91. Decode Ways](https://leetcode.com/problems/decode-ways/) | [javascript]()|[:memo:](https://leetcode.com/problems/decode-ways/)|Medium|
182+
183+
|Day 80| [1345. Jump Game IV](https://leetcode.com/problems/jump-game-iv/) | [javascript]()|[:memo:](https://leetcode.com/problems/jump-game-iv/)|Hard|

0 commit comments

Comments
 (0)