Jump Game, Leetcode 解题笔记

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.

一维DP,记录当前能达到的最远值。

public class Solution {
    public boolean canJump(int[] A) {
        int maxCover = 0;
        for(int i = 0; i < A.length && i <= maxCover; i++){
            if(A[i] + i > maxCover)
                maxCover = i+A[i];
            if(maxCover >= A.length-1)
                return true;
        }
        return false;
    }
}
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s