|
| 1 | +// 🚀 Greedy approach: much more efficient than the recursive approach |
| 2 | +// Time Complexity: O(n) |
| 3 | +// Space Complexity: O(1), No extra memory used |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {number[]} nums |
| 7 | + * @return {boolean} |
| 8 | + */ |
| 9 | +var canJump = function (nums) { |
| 10 | + // ➡️ The farthest position you can reach from any of the previous indices you have already visited. |
| 11 | + let farthest = 0; |
| 12 | + |
| 13 | + for (let i = 0; i < nums.length; i++) { |
| 14 | + // You cannot reach this position even with your farthest jump value. |
| 15 | + if (i > farthest) return false; |
| 16 | + |
| 17 | + // Compare current maximum jump with the previous maximum. |
| 18 | + farthest = Math.max(farthest, nums[i] + i); |
| 19 | + |
| 20 | + // Check if you can reach the last index with the current farthest jump. |
| 21 | + if (farthest >= nums.length - 1) return true; |
| 22 | + } |
| 23 | + return false; |
| 24 | +}; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {number[]} nums |
| 29 | + * @return {boolean} |
| 30 | + */ |
| 31 | + |
| 32 | +// Time Complexity: O(n) |
| 33 | +// Space Complexity: O(n) |
| 34 | +var canJump = function (nums) { |
| 35 | + let lastIndex = nums.length - 1; |
| 36 | + |
| 37 | + // Initialize memoization array to track visited indices |
| 38 | + let memo = new Array(nums.length).fill(undefined); |
| 39 | + |
| 40 | + const dfs = (i) => { |
| 41 | + // Base case: if we've reached or surpassed the last index, return true |
| 42 | + if (i >= lastIndex) return true; |
| 43 | + |
| 44 | + // If the current index has already been visited, return the stored result |
| 45 | + if (memo[i] !== undefined) return memo[i]; |
| 46 | + |
| 47 | + // Calculate the farthest position that can be reached from the current index |
| 48 | + let maxJump = Math.min(nums[i] + i, lastIndex); |
| 49 | + |
| 50 | + for (let j = i + 1; j <= maxJump; j++) { |
| 51 | + if (dfs(j)) { |
| 52 | + memo[i] = true; |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + memo[i] = false; |
| 58 | + return false; |
| 59 | + }; |
| 60 | + |
| 61 | + return dfs(0); |
| 62 | +}; |
| 63 | + |
| 64 | + |
| 65 | +// 🌀 recursive approach |
| 66 | +// ⚠️ Time Complexity: O(2^n) - Exponential due to recursive branching without memoization |
| 67 | +// 🔵 Space Complexity: O(n) - Recursive call stack depth |
| 68 | + |
| 69 | +/** |
| 70 | + * Check if you can jump to the last index from the first index. |
| 71 | + * @param {number[]} nums - Array where nums[i] is the max jump length from position i. |
| 72 | + * @return {boolean} True if you can reach the last index, otherwise false. |
| 73 | + */ |
| 74 | + |
| 75 | +// var canJump = function (nums) { |
| 76 | +// const dfs = (start) => { |
| 77 | +// // Base Case: Reached the last index |
| 78 | +// if (start === nums.length - 1) { |
| 79 | +// return true; |
| 80 | +// } |
| 81 | + |
| 82 | +// // Recursive Case: Try all possible jumps |
| 83 | +// for (let i = 1; i <= nums[start]; i++) { |
| 84 | +// // Example with nums = [2, 3, 1, 1, 4]: |
| 85 | +// // start = 1, nums[1] = 3 (can jump 1, 2, or 3 steps) |
| 86 | +// // Possible calls: |
| 87 | +// // dfs(1 + 1) -> check from index 2 |
| 88 | +// // dfs(1 + 2) -> check from index 3 |
| 89 | +// // dfs(1 + 3) -> reached the last index (success) |
| 90 | + |
| 91 | +// if (dfs(start + i)) { |
| 92 | +// return true; |
| 93 | +// } |
| 94 | +// } |
| 95 | + |
| 96 | +// return false; // cannot reach the last index |
| 97 | +// }; |
| 98 | + |
| 99 | +// return dfs(0); |
| 100 | +// }; |
| 101 | + |
0 commit comments