116.Jump Game
1.Description(Medium)
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.
Notice
This problem have two method which isGreedy
andDynamic Programming
.
The time complexity ofGreedy
method isO(n)
.
The time complexity ofDynamic
Programming method isO(n^2)
.
We manually set the small data set to allow you pass the test in both ways. This is just to let you learn how to use this problem in dynamic programming ways. If you finish it in dynamic programming ways, you can try greedy method to make it accept again.
Example
A =[2,3,1,1,4]
, returntrue
.
A =[3,2,1,0,4]
, returnfalse
.
2.Code
两个解法:1> Greedy O(n) 2>Dynamic Programming O(n*n)
DP:
state: can[i] 表示是否能跳到i
initialize: can[0]=true;
function:can[j]==true && j+A[j]>=i
answer: can[A.length-1]
Last updated