題目:給定一個非負整數數組,你最初位於數組的第一個位置。 數組中的每個元素代表你在該位置可以跳躍的最大長度。 你的目標是使用最少的跳躍次數到達數組的最后一個位置。
思路:設定一個邊界,看看哪種方式可以跳的方式最遠。
程序:
class Solution:
def jump(self, nums: List[int]) -> int:
length = len(nums)
if length <= 1:
return 0
boudary = 0
auxiliary_length = 0
step = 0
for index in range(length):
auxiliary_length = max(auxiliary_length, nums[index] + index)
if index == boudary:
boudary = auxiliary_length
step += 1
if boudary == length - 1:
break
return step