Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.

Solution: Updated solution: try every reachable index.

 1 class Solution {
 2 public:
 3     bool canJump(int A[], int n) {
 4         int start = 0, end = 0;
 5         while(start <= end && end < n - 1) 
 6         {
 7             end = max(end, start + A[start]);
 8             start++;
 9         }
10         return end >= n - 1;
11     }
12 };

 

posted @ 2014-04-05 00:39  beehard  阅读(134)  评论(0编辑  收藏  举报