Jump Game II LeetCode solution in JavaScript

1
/*
2
* Time Complexity : O(n)
3
* Space Complexity: O(1)
4
* */
5
var jump = function (nums) {
6
var jump = 0;
7
var prev = 0;
8
var max = 0;
9
for (var i = 0; i < nums.length - 1; i++) {
10
// Keep track of the maximum jump
11
max = Math.max(max, i + nums[i]);
12
// When we get to the index where we had our previous maximum jump, we increase our jump...
13
if (i === prev) {
14
jump++;
15
prev = max;
16
}
17
}
18

19
return jump;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0