JavaScript solution with O(n) time and O(1) space


  • 0
    L
    function jump(nums) {
        var max = 0;
        var nextMax = 0;
        var jumps = 0;
    
        nums.some((v, i) => {
            if (max >= nums.length - 1) {
                return true;
            }
    
            nextMax = Math.max(i + v, nextMax);
            
            if (i === max) {
                max = nextMax;
                jumps++;
            }
        });
        
        return jumps;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.