Description
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.
Example:
1 | Input: [2,3,1,1,4] |
Note:
You can assume that you can always reach the last index.
Difficulty: Hard
Code:
1 | class Solution { |
题意
给定一个正数数组,初始化时指向数组的第一个元素,每个元素的值表示在这个位置上能向前跳的最大长度。
目标是跳动到最后一个元素,并且用到步数最少,并返回最少步数。假定一定能够到达最后一个元素。
Solution 1
使用动态规划,令len表示nums第长度,有dp[len],dp[i]表示从数组第i位跳到数组最后一位所需第最少步数。dp[len-1]=0,因为数组最后一位不需要跳。
从数组第倒数第二位开始往前遍历,取出当前位置i上的数字steps,初始化当前位置i用的最少步数min为int最大值。在位置i上最多有steps中跳法,但同时要保证不会跳出数组边界。
假定从位置i跳j步到位置i+j上,若dp[i+j]不能到达数组末尾即dp[i+j]为int最大值,则j加1。否则此时所用步数为1+dp[i+j],更新min到值。
所有可能到步数遍历完后,确定位置i对应所需到步数dp[i]。最终结果返回dp[0]。
1 | class Solution { |
时间复杂度: O()。
空间复杂度: O()。
Solution 2
贪婪算法,jumps代表跳到步数,curEnd表示当前能跳到点的范围是[curBegin, curEnd],curFarthest表示在[curBegin, curEnd]下一次跳所能到达的最远的点。
遍历数组,i为数组当前所在点,则该点能最远到达的点为i+nums[i],并据此更新curFarthest。
当i到达curEnd,表示[curBegin, curEnd]所能到达的点都已检查完毕,下次跳最远能跳到curFarthest。
那么将跳一次jumps++,同时此时能跳的最远位置curEnd更新为curFarthest,继续循环。
1 | class Solution { |
时间复杂度: O()。
空间复杂度: O()。