Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 878 Bytes

File metadata and controls

40 lines (34 loc) · 878 Bytes

Screen Shot 2022-10-09 at 12 22 14 AM

C++

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int maxStep = 0;
        for(int i = 0; i < nums.size(); i++) {
            if(i > maxStep) {
                return false;
            }
            maxStep = max(maxStep, nums[i] + i);
        }
        return true;
    }
};
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function(nums) {
    let furthest = 0;
    let currentJumpEnd = 0;
    
    for(let i = 0; i < nums.length - 1; i++) {
        furthest = Math.max(furthest, i + nums[i]);
        if(i === currentJumpEnd) {
            currentJumpEnd = furthest
        }
    }
    return currentJumpEnd >= nums.length - 1
};
``