MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / jump

Function jump

greedy/45-jump-game.js:7–20  ·  view source on GitHub ↗
(nums)

Source from the content-addressed store, hash-verified

5 * Space O(1)
6 */
7var jump = function (nums) {
8 if (nums.length <= 1) return 0;
9 let jumps = 0;
10 let maxReach = 0;
11 let steps = 0;
12 for (let i = 0; i < nums.length - 1; i++) {
13 maxReach = Math.max(maxReach, i + nums[i]);
14 if (i == steps) {
15 jumps++;
16 steps = maxReach;
17 }
18 }
19 return jumps;
20};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected