-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump-game.js
43 lines (36 loc) · 911 Bytes
/
jump-game.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
/*
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
*
* Each element in the array represents your maximum jump length at that position.
*
* Determine if you are able to reach the last index.
*
* Example
* Input: [2,3,1,1,4]
* Output: true
* https://leetcode.com/explore/interview/card/top-interview-questions-medium/111/dynamic-programming/807/
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
const canReach = (arr, idx = 0, mem = {}) => {
if (idx === arr.length - 1) {
return true;
}
let jmps = arr[idx];
mem[idx] = true;
while (jmps > 0) {
if (mem[idx + jmps]) {
break;
}
if (canReach(arr, idx + jmps, mem)) {
return true;
}
jmps -= 1;
}
return false;
};
export const canJump = function (nums) {
return canReach(nums);
};