Skip to content

Commit 4189e0c

Browse files
authored
Update 1340-jump-game-v.js
1 parent e49257f commit 4189e0c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

1340-jump-game-v.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} d
4+
* @return {number}
5+
*/
6+
const maxJumps = function (arr, d) {
7+
const map = {}
8+
let max = 1
9+
for (let i = 0; i < arr.length; i++) {
10+
max = Math.max(max, calc(i))
11+
}
12+
return max
13+
14+
function calc(i) {
15+
if (map[i]) return map[i]
16+
let max = 1
17+
const left = Math.max(0, i - d)
18+
for (let j = i - 1; j >= left; j--) {
19+
if (arr[j] >= arr[i]) break
20+
if (arr[j] < arr[i]) max = Math.max(max, calc(j) + 1)
21+
}
22+
const right = Math.min(arr.length - 1, i + d)
23+
for (let j = i + 1; j <= right; j++) {
24+
if (arr[j] >= arr[i]) break
25+
if (arr[j] < arr[i]) max = Math.max(max, calc(j) + 1)
26+
}
27+
map[i] = max
28+
return map[i]
29+
}
30+
}
31+
32+
// another
33+
134
/**
235
* @param {number[]} arr
336
* @param {number} d

0 commit comments

Comments
 (0)