#C1361. Minimum Jumps to Reach End

    ID: 43167 Type: Default 1000ms 256MiB

Minimum Jumps to Reach End

Minimum Jumps to Reach End

You are given an array of non-negative integers. Each element in the array represents the maximum number of steps you can jump forward from that position. Your task is to determine the minimum number of jumps required to reach the last index of the array starting from the first index.

For example, given the array [2, 3, 1, 1, 4], the minimum number of jumps to reach the end is 2 (jump from index 0 to index 1, then from index 1 to index 4).

The problem can be stated mathematically as follows: Let \(a\) be the array with length \(n\), and let \(J(i)\) denote the minimum number of jumps needed to reach index \(n-1\) from index \(i\). For an index \(i\), you can jump to any index \(j\) such that \(i < j \le i + a_i\). Then one way to represent this is:

\( J(i) = 1 + \min_{i < j \le i + a_i} \{J(j)\} \)

You may assume that it is always possible to reach the last index.

inputFormat

The input consists of two lines:

  • The first line contains a single integer \(n\) representing the number of elements in the array.
  • The second line contains \(n\) space-separated non-negative integers, where the \(i\)-th integer represents the maximum jump length from index \(i\).

outputFormat

Output a single integer, which is the minimum number of jumps required to reach the last index of the array.

## sample
5
2 3 1 1 4
2

</p>