We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 569d22e commit ee698d2Copy full SHA for ee698d2
jump-game/yolophg.js
@@ -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