Skip to content

Commit ee698d2

Browse files
committed
Add week 12 solutions: jumpGame
1 parent 569d22e commit ee698d2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

jump-game/yolophg.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var canJump = function (nums) {
5+
// start from the last position
6+
let lastPos = nums.length - 1;
7+
8+
// traverse the array from the end to the start
9+
for (let i = nums.length - 2; i >= 0; i--) {
10+
// if can jump from the current position to the lastPos or beyond
11+
if (i + nums[i] >= lastPos) {
12+
// move the lastPos to the current position
13+
lastPos = i;
14+
}
15+
}
16+
17+
// if have moved lastPos to the start, can reach the end
18+
return lastPos === 0;
19+
};

0 commit comments

Comments
 (0)