DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Jump Game - I

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function (nums) {
  let maximum = 0;
  if (nums.length <= 1) {
    return true;
  }

  for (let i = 0; i < nums.length; i++) {
    if (maximum <= i && nums[i] == 0) return false;

    if (i + nums[i] > maximum) {
      maximum = i + nums[i];
    }

    if (maximum >= nums.length - 1) return true;
  }
  return false;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)