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.

01 bool canJump(int A[], int n) {
02      int prv=0;
03      int far=0;
04      int max=0;
05      while(true){
06         for(int i=prv;i<=far;i++){
07              if(max<i+A[i])
08                   max=i+A[i];
09         }
10         if(max>=n1)
11              return true;
12         if(max==far)
13              return false;
14         prv=far;
15         far=max;
16      }
17 }
This entry was posted in Dynamic programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s