code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

045 Jump Game 2

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const jumpGame2 = nums => {
  let left = 0
  let right = 0
  let result = 0
  while (right < nums.length - 1) {
    let max = 0
    for (let i = left; i < right + 1; i++) {
      max = Math.max(max, i + nums[i])
    }
    left = right + 1
    right = max
    result += 1
  }
  return result
}

console.log(jumpGame2([2, 3, 1, 1, 4]))

Created 2023-05-13T16:38:59+01:00 · Edit