Skip to content

Commit b867094

Browse files
authored
Create 2770-maximum-number-of-jumps-to-reach-the-last-index.js
1 parent 89ab685 commit b867094

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var maximumJumps = function(nums, target) {
7+
const n = nums.length
8+
const dp = Array(n).fill(-1)
9+
dp[0] = 0
10+
for(let i = 0; i < n; i++) {
11+
if(dp[i] === -1) continue
12+
for(let j = i + 1; j < n; j++) {
13+
if (nums[j] - nums[i] <= target && nums[j] - nums[i] >= -target) {
14+
dp[j] = Math.max(dp[j], dp[i] + 1)
15+
}
16+
}
17+
}
18+
19+
return dp.at(-1)
20+
};
21+

0 commit comments

Comments
 (0)