From 42c0ba4f651ddf4f40bb066f108074069d15acbc Mon Sep 17 00:00:00 2001 From: wogha95 Date: Mon, 21 Oct 2024 23:20:14 +0900 Subject: [PATCH] solve: jump game --- jump-game/wogha95.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 jump-game/wogha95.js diff --git a/jump-game/wogha95.js b/jump-game/wogha95.js new file mode 100644 index 000000000..1d102141b --- /dev/null +++ b/jump-game/wogha95.js @@ -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; +};