原文地址http://www.lintcode.com/problem/jump-game-ii/
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. Your goal is to reach the last index in the minimum number of jumps. For example: Given array A = [2,3,1,1,4] 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.)
翻譯:給定一個非負(fù)整數(shù)數(shù)組,您首先被定位在數(shù)組的第一個索引處。數(shù)組中的每個元素都代表你在該位置上的最大跳轉(zhuǎn)長度。你的目標(biāo)是達(dá)到最小跳數(shù)的最后一個指數(shù)。例如:給定的數(shù)組a [ 2,3,1,1,4 ]跳到最后一個索引的最小數(shù)目是2。(從指數(shù)0上升到1,然后是最后一個指數(shù)的3個步驟。)
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
p = [0]
for i in range(len(nums) - 1):
while(i + nums[i] >= len(p) and len(p) < len(nums)):
p.append(p[i] + 1)
return p[-1]