45. Jump Game II
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.解题要点:
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = [len(nums)] * len(nums)
dp[0] = 0
for i in range(len(nums)):
for j in range(i):
if i <= j + nums[j]:
dp[i] = min(dp[i], dp[j] + 1)
return dp[-1]
Last updated