Jason Koo

      Stay hungry, Stay foolish!

导航

Amazon 2013-09-27 Question 2

Posted on 2013-09-27 11:09  Jason Koo  阅读(116)  评论(0编辑  收藏  举报
Question 2 / 2 (Amazon Campus(3): walk to the array)

Given an array of integers, each element represents the max number of jumps that you can move forward.
Write a piece of code to find out the minimum number of elements you need to select to reach the end of the array (starting from the first element).
• All integers is equal to or greater than 0
• If an element is 0, you cannot make any jumps
• -1 should be output if it is unable to reach the end of the array
Here is an example to illustrate the question.
Sample Input: 1,3,5,2,9,3,1,1,8
Sample Output: 3
Explanation:
Here the min # of selections is : 3
with the sequence : 1-> 3 -> 9 ->8
First element is 1, so can only go to 3.
Second element is 3, so can make at most 3 jumps: eg to 5 or 2 or 9.

 

Input format:

the first line is a number indicating the size of the array, then the second line follows the array, seperated by space.

Example:

9

1 3 5 2 9 3 1 1 8