【LeetCode】解题45:Jump Game II

跳跃游戏 II-leetcode45-贪心-java题解 就这样 阅读详情

Problem 45: Jump Game II [Hard]

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:

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.

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

来源:LeetCode

解题思路

1. 暴力搜索(不剪枝)

从后往前遍历数组,计算每个位置能够到达数组末尾的最小跳跃次数。
具体方法: 由于从后往前遍历,因此位于每个位置后方的跳跃点都已经计算过最小跳跃次数,只需要在当前位置能够跳跃的范围内搜索一个最小值,再加上1,即为当前位置的最小跳跃次数。一直遍历到nums[0]就能算出从数组头部到末尾的最小跳跃次数。
缺点: 该方法计算出了每一个位置上的最小跳跃次数,没有剪去一些不必要的搜索过程,最坏情况下复杂度为 1 2 n 2 \frac{1}{2}n^2 21n2.

2. 贪心算法

利用贪心算法的思想:
一个点选择下一个跳跃点的原则为:下一个跳跃点拥有最广的视野。可以解释为,它在当前点可选择范围内能够拥有最广的未来跳跃选择,那么它的跳跃次数一定最小。也就是在每个位置index的搜索过程中,选择下一个跳跃点的公式为:
n e x t _ p o i n t = arg max ⁡ i { i + n u m s [ i ] } next\_point = \argmax_i\{i + nums[i]\}\\ next_point=iargmax{i+nums[i]}
其中, i ∈ [ i n d e x + 1 , i n d e x + n u m s [ i n d e x ] ] i \in [index + 1, index + nums[index]] i[index+1,index+nums[index]],然后对 n e x t _ p o i n t next\_point next_point进行递归搜索。
优点: 每一次搜索都只选择一个点进行递归搜索,可以省去暴力搜索中的大部分搜索过程,事实证明这些搜索过程都是不必要的。使用贪心算法之后,时间复杂度减小为O(n)。

具体过程:

  • 使用递归搜索,从index=0开始,直到index + nums[index] >= N-1时结束,每次递归深度增加时,返回的跳跃步数+1。
  • 在每个位置index的搜索过程中,搜索范围为当前位置能够跳跃的范围(index+1 ~ index + nums[index]),在该范围中找到一个能够在未来跳得最远的点,作为下一层递归的起始点。
  • 递归结束条件:
    a. 当前点能够一步跳到数组末尾(index + nums[index] >= N-1),直接返回1。
    b. 在搜索下一个跳跃点时,发现跳跃范围内有点可以一步跳到数组末尾,直接返回2。

运行结果:
在这里插入图片描述

Solution (Java)

暴力搜索:

class Solution {
    public int jump(int[] nums) {
        int N = nums.length;
        if (N == 0 || N == 1) return 0;
        if (N == 2) return 1;
        int[] jumps = new int[N];
        jumps[N - 1] = 0;
        for(int i = N - 2; i >= 0; i--){
            if(nums[i] == 0){
                jumps[i] = N;
            }
            else if(i + nums[i] >= N - 1){
                jumps[i] = 1;
            }
            else{
                int min = N;
                for(int j = i + 1; j <= i + nums[i]; j++){
                    if(jumps[j] < min){
                        min = jumps[j];
                    }
                }
                jumps[i] = min + 1;
            }
        }
        return jumps[0];
    }
}

贪心算法:

class Solution {
    public int jump(int[] nums) {
        int N = nums.length;
        if (N == 0 || N == 1) return 0;
        if (N == 2) return 1;
        return maxCover(0, nums, N);
    }
    protected int maxCover(int start, int[] nums, int N){
        if(start + nums[start] >= N - 1) return 1;
        int max = 0;
        int next_start = start;
        for(int i = start + 1; i <= start + nums[start]; i++){
            if(i + nums[i] >= N - 1) return 2;
            if(i + nums[i] > max){
                max = i + nums[i];
                next_start = i;
            }
        }
        return maxCover(next_start, nums, N) + 1;
    }
}

修改过程

  • 第一次提交了暴力搜索的解法,系统判时335ms,修改为贪心算法后只需1ms。
LeetCode45题-跳跃游戏||-java实现-图解思路与手撕代码 阅读详情

相关推荐

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

wengyunpeng的博客 356

leetcode解题45. Jump Game II&55. Jump Game java (跳跃游戏)

leetcode解题45. Jump Game II&55. Jump Game java (跳跃游戏)。动态规划,贪心。

mine_song的博客 3716

leetcode 45. Jump Game II (dp)

https://leetcode.cn/problems/jump-game-ii/首先,这道题我想到了用map来解决,其实整型数组也可以,但还要考虑长度的问题有点麻烦就算了(其实就是懒) 这个解法看着很乱,我大致解释一下,就是pre每一层存储的都是上一条的最优结点,flag是其对应的最远距离。如果超出了上一条的最大辐射范围,就开启一次新的跳跃。但是,这个思路完全不需要map或者数组,其实只需要维护几个整型变量即可,所以就有了解法2 具体的解释我都放在注释里了,但还是好长啊,根本没有大佬那种几行代码解决问题

ljhsq的博客 381

LeetCode 45. Jump Game II 题解

本题是贪心算法的经典问题,主要考察对贪心思想的理解和应用。通过维护当前跳跃的结束位置和能够到达的最远位置,我们可以高效地计算出到达最后一个下标所需的最小跳跃次数。贪心算法的核心思想是:在每一步都做出当前情况下的最优选择,从而希望最终得到全局最优解。在本题中,我们的最优选择是在每一次跳跃中尽可能地覆盖更远的距离。这种方法不仅适用于跳跃游戏 II 问题,还可以应用于许多其他需要贪心策略的问题,例如加油站问题、分发饼干问题等。掌握贪心算法的思想,对于解决这类问题非常重要。

cannonjinx的博客 3604

LeetCode解题55:Jump Game

LeetCode解题 55:Jump Game Problem 55: Jump Game [Medium]解题思路Solution (Java) Problem 55: Jump Game [Medium] Given an array of non-negative integers, you are initially positioned at the first index of the...

Fayedily的博客 287

LeetCode】55. Jump Game 解题报告(Python)

题目分析: 这一题是让我们以某个数值位置跳跃,值为跳跃步数,看是否能跳跃到最后,与【LeetCode45. Jump Game II 一样,还要简单一些,我们直接借助这个思路即可。思路就是从某个位置i+num[i]相加一直取大,看最后是否大于等于nums的length-1。 测试代码: class Solution: def canJump(self, nums): ne...

L141210113的专栏 640

LeetCode 45. 跳跃游戏 IIJump Game II

易错点正确做法说明每次跳一步题目要求跳跃次数最少,需贪心跳最远在最后位置仍判断边界不需要遍历到n-1,提前终止更高效cur_end和further搞混cur_end是当前跳跃边界,further是下一跳能到的最远位置本题是跳跃类问题的进阶题,要求的是最少跳跃次数用贪心思想 + BFS 层次遍历模拟是最优解法。

2501_91641025的博客 718

LeetCode - 解题笔记 - 45 - Jump Game II

Jump Game II Solution 1 乍一看想到动态规划去了,才发现我忽略了最大步长的信息(没有的话就是DP了),因此不需要考虑跳多了的情况,而且体面能够保证绝对能到达,所以不用担心0步长的情形(跳不出去啦!)。 因此本题可以直接使用贪心算法:向后遍历,找出能够跳的最远的选择。 贪心最麻烦的地方就是全局最优解的证明,我这里给出一个简单的思路:从同一个起点出发,如果当前选择不是贪心选择的,那么下一个选择必然比贪心方案跳的更近。完成一次跳跃后有两种情形:贪心方案的第二次选择的位置是否存在于当前方案的第

支锦铭的博客 727

JAVA算法Jump Game 45题和55题算法详解

LeetCode45题和55题是关于Jump Game的问题,下面来看看这两道题目的求解方法。 这两个题目的区别是:55题的要求判断你是否能够从开始位置跳到结束位置;而45题的要求是求你从开始位置能够跳到结束位置的最小跳跃次数。。 原题链接: 55.Jump Gamehttps://leetcode.com/problems/jump-game/ Given an array of...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 478

leetcode刷题】45 & 55 jump game & jump game 2

原题链接 https://leetcode.com/problems/jump-game/ https://leetcode.com/problems/jump-game-ii/ 解题思路 两道题思路类似,都是用贪心算法。记录下跳i步时可以跳过的最大距离。但是注意,在确定第i+1步的最远距离时,需要考虑所有i-1步到i步之间范围起始点,跳两步寻找i+1步的最远距离。 对于jump game,当st...

Arwen的博客 153

Leetcode Algorithm 045. Jump Game II

Leetcode Algorithm 045. Jump Game II 给定一个非负整数的数组,数组中的元素表示从当前位置可以跳过的最长步数;从数组的第一个位置,可以经过中途的某些点跳到最后一个位置,请找出少的跳跃次数

Law’s Blog 461

leetcodeJump Game系列

55. Jump Game 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. ...

ghscarecrow的博客 269

leetcode题集: 45. Jump Game II

题目大意:就是给定一个非负数组,求到最后一个的最小跳数。每次跳动范围不超过当前索引的数值。举个栗子,当前在0号位置,最多只能跳到+2,也就是2号位置。 题目分析:第一感觉,就是个动态规划嘛。很快动态转移方程:dp[j] = min(dp[j], dp[i]+1) ,i = 0....j。于是很快写出了以下代码: class Solution { public: int jump(...

wx_14678的博客 212

刷题记录(Jump Game II

Leecode 45Jump Game II (JS答题) 解法一: function jump(nums: number[]): number { var end:number = 0; var maxPosition:number = 0; var resCount:number = 0; for(var i = 0; i < nums.length-1; i++){ maxPosition = Math.max(maxPosition, i

下半年记忆的博客 142

LeetCode: 45. Jump Game II

LeetCode: 45. Jump Game II

杨领well的专栏 989

LEETCODE 45. Jump Game II

LEETCODE 45. Jump Game II题目大意给出一个正整数数组,每个元素代表当前位置所能跳转的最大步数,求能到达数组最后的的最少跳转次数,实际上是44题的一个变种,44题只是判断能否到达数组最后,本题多加一个要记录最少跳转次数的要求。 例如 A = [2,3,1,1,4] The minimum number of jumps to reach the last index is

默默 340

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

希望的专栏 279
上一篇: 【LeetCode】解题42:Trapping Rain Water
下一篇: 【LeetCode】解题48:Rotate Image
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值