-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
43 lines (42 loc) · 1.11 KB
/
solution.js
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
/**
* @param {number[]} arr
* @param {number} d
* @return {number}
*/
var maxJumps = function (arr, d) {
const results = new Array(arr.length).fill(0);
for (let i = 0; i < arr.length; i++) {
helper(arr, i, d, results);
}
return Math.max(...results);
};
// 直接按记忆化递归实现了
function helper (arr, index, d, results) {
if (results[index] !== 0) {
return results[index];
}
results[index] = 1;
for (let i = 1; i <= d; i++) {
if (index + i === arr.length) {
break;
}
if (arr[index + i] >= arr[index]) {
break;
}
if (arr[index + i] < arr[index]) {
results[index] = Math.max(results[index], helper(arr, index + i, d, results) + 1);
}
}
for (let i = 1; i <= d; i++) {
if (index - i < 0) {
break;
}
if (arr[index - i] >= arr[index]) {
break;
}
if (arr[index - i] < arr[index]) {
results[index] = Math.max(results[index], helper(arr, index - i, d, results) + 1);
}
}
return results[index];
}