-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump-game.js
39 lines (31 loc) · 966 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
const canJump = (nums) => {
let furthest = 0;
for (let index = 0; index < nums.length && index <= furthest; index++) {
furthest = Math.max(furthest, index + nums[index]);
if (furthest >= nums.length - 1) return true;
}
return false;
}
// April 4, 2023
const canJump = nums => {
let maxIndex = nums[0];
for (let index = 0; index < nums.length; index++) {
if (maxIndex < index)
return false;
maxIndex = Math.max(maxIndex, index + nums[index]);
}
return maxIndex >= nums.length - 1;
}
let nums, expected, actual;
nums = [2, 3, 1, 1, 4];
expected = true;
actual = canJump(nums);
console.assert(expected === actual, '%o', { nums, expected, actual });
nums = [3, 2, 1, 0, 4];
expected = false;
actual = canJump(nums);
console.assert(expected === actual, '%o', { nums, expected, actual });
nums = [0, 2, 1, 0, 4];
expected = false;
actual = canJump(nums);
console.assert(expected === actual, '%o', { nums, expected, actual });