[45] 跳跃游戏 II

/**
 * @param {number[]} nums
 * @return {number}
 */
const jump = function (nums) {
  if (nums.length === 1) return 0;
  let ans = 0;
  let left = 0;
  let right = nums[0] + 1;
  while (right < nums.length) {
    ans = ans + 1;
    let newRight = left + nums[left] + 1;
    for (let j = left; j < right; j++) {
      newRight = Math.max(j + nums[j], newRight);
    }
    left = left + nums[left];
    right = newRight + 1;
  }
  return ans + 1;
};

 

posted @ 2023-11-30 13:57  人恒过  阅读(19)  评论(0)    收藏  举报