45. Jump game II

int jump(int *nums, int numsSize) {
  int end = 0, far = 0, count = 0;

  for (int i = 0; i < numsSize - 1; i++) {
    if (i + nums[i] > far)
      far = i + nums[i];
    if (i == end) {
      // I've used up everything reachable with k jumps,
      // so now I'm entering the area that needs k+1 jumps.
      count++;
      end = far;
    }
  }

  return count;
}
#include <algorithm>
#include <vector>

using namespace std;

class Solution {
public:
  int jump(vector<int> &nums) {
    int end = 0, far = 0, count = 0;

    for (int i = 0; i < nums.size() - 1; i++) {
      far = max(far, i + nums[i]);
      if (i == end) {
        // I’ve used up everything reachable with k jumps,
        // so now I’m entering the area that needs k+1 jumps.
        count++;
        end = far;
      }
    }

    return count;
  }
};
package greedy

func jump(nums []int) int {
    end, far, count := 0, 0, 0

    for i := 0; i < len(nums)-1; i++ {
        far = max(far, i+nums[i])
        if i == end {
            // I've used up everything reachable with k jumps,
            // so now I'm entering the area that needs k+1 jumps.
            count++
            end = far
        }
    }

    return count
}
class Solution {
    public int jump(int[] nums) {
        int end = 0, far = 0, count = 0;

        for (int i = 0; i < nums.length - 1; i++) {
            far = Math.max(far, i + nums[i]);
            if (i == end) {
                // I've used up everything reachable with k jumps,
                // so now I'm entering the area that needs k+1 jumps.
                count++;
                end = far;
            }
        }

        return count;
    }
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var jump = function (nums) {
  let end = 0,
    far = 0,
    count = 0;

  for (let i = 0; i < nums.length - 1; i++) {
    far = Math.max(far, i + nums[i]);
    if (i === end) {
      // I've used up everything reachable with k jumps,
      // so now I'm entering the area that needs k+1 jumps.
      count++;
      end = far;
    }
  }

  return count;
};
from typing import List


class Solution:
    def jump(self, nums: List[int]) -> int:
        end = far = count = 0

        for i in range(len(nums) - 1):
            far = max(far, i + nums[i])
            if i == end:
                # I've used up everything reachable with k jumps,
                # so now I'm entering the area that needs k+1 jumps.
                count += 1
                end = far

        return count
impl Solution {
    pub fn jump(nums: Vec<i32>) -> i32 {
        let (mut end, mut far, mut count) = (0, 0, 0);

        for i in 0..nums.len().saturating_sub(1) {
            far = far.max(i + nums[i] as usize);
            if i == end {
                // I've used up everything reachable with k jumps,
                // so now I'm entering the area that needs k+1 jumps.
                count += 1;
                end = far;
            }
        }

        count
    }
}