Problem 137
Jump Game
Medium · 20 pts · Array, Greedy, Dynamic Programming
Problem
You start at index 0 of an array of N non-negative integers. The value at each position is the maximum length of a jump you can make from there: from index i you can jump to any index from i + 1 to i + A[i]. Decide whether you can reach the last index. A greedy scan is enough: track the farthest index reachable so far. Walk through the array, and if you ever stand at an index beyond that farthest point, you are stuck. Otherwise extend the farthest point with i + A[i].
Input
First line: integer N. Second line: N space-separated integers.
Output
Print YES or NO.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 100000
Example input
5 2 3 1 1 4
Example output
YES
Explanation
In 2 3 1 1 4, jump from index 0 to index 1 (value 3), and from there jump 3 steps to index 4, the last index. So the answer is YES.