Skip to content

Commit

Permalink
solve: jump game
Browse files Browse the repository at this point in the history
  • Loading branch information
wogha95 committed Oct 21, 2024
1 parent b10edd3 commit 42c0ba4
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions jump-game/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* TC: O(N)
* SC: O(1)
* N: nums.length
*/

/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function (nums) {
if (nums.length === 1) {
return true;
}

let maximumIndex = 0;

for (let index = 0; index < nums.length; index++) {
const jumpLength = nums[index];

if (jumpLength === 0) {
continue;
}

if (maximumIndex < index) {
return false;
}

maximumIndex = Math.max(maximumIndex, index + nums[index]);

if (maximumIndex >= nums.length - 1) {
return true;
}
}

return false;
};

0 comments on commit 42c0ba4

Please sign in to comment.