【一天一道LeetCode】#45. Jump Game II

一天一道LeetCode系列

(一)题目

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.)

Note:

You can assume that you can always reach the last index.

(二)解题

1、首先想到的做法就是对于序号i,判断i+nums[i]能不能到达终点,如果能直接return,如果不能对小于nums[i]的数依次进行递归,得到最小的min值。这种做法直接超时了,TLE!

class Solution {
public:
    int min;
    int jump(vector<int>& nums) {
        min = nums.size();
        jumpdfs(nums,0,0);
        return min;
    }
    void jumpdfs(vector<int>& nums , int idx , int count)
    {
        int len = nums.size();
        if(idx >= len -1) {
            min = count;
            return;
        }
        if(idx<len && idx + nums[idx] >= len-1){
            min = min>(count+1)?(count+1):min;
            return;
        }
        else
        {
            for(int i = 1 ; idx<len && i<= nums[idx] ; i++)
            {
                jumpdfs(nums,idx+i,count+1);
            }
        }
    }

};

2、然后看到提示中有提到贪心算法,于是联想到昨天做的通配符那题,可以记录上一跳能达到的最远位置,和当前跳所能到达的最远位置,每次循环就更新这两个值,直到上一跳能达到终点就退出。

/*
思路:贪心算法是假定每一次都作出当前的最优解,所以对于本题,每次记录当前的最远位置,和上一跳的最远位置。
*/
class Solution {
public:
    int jump(vector<int>& nums) {
        int ret = 0 ;
        int last = 0;
        int cur = 0;
        for(int i = 0 ; i < nums.size() ; i++)
        {
            if(last>=nums.size()-1) break;
            if(i>last)//如果当前位置已经跳出了上一跳的最远位置
            {
                last = cur;//更新上一跳的最远位置等于当前跳的最远位置
                ++ret;//跳的次数加1
            }
            cur = max(cur , i+nums[i]);//更新当前跳的最远位置
        }
        return ret;
    }
};
时间: 2024-10-10 15:52:32

【一天一道LeetCode】#45. Jump Game II的相关文章

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

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 nu

LeetCode 45 Jump Game II(按照数组进行移动)

题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. 求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数! 1.当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1 2.初始化left=0 right = nums[0] 当left<=right时进入循环体中. 3.从第i=0开始,如果当前跳动距离大于数组长度则返回st

leetcode 45 Jump Game II ---- java

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

[LeetCode] #45 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

19.2.7 [LeetCode 45] 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

【LeetCode】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

45. Jump Game II(js)

45. 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 minimu

LeetCode 045 Jump Game II

题目要求: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 minim

55 Jump Game i &amp;&amp; 45 Jump Game ii

Jump Game Problem statement: 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