首页 > 其他分享 >[LeetCode]Jump Game II

[LeetCode]Jump Game II

时间:2023-02-02 15:35:36浏览次数:41  
标签:II return index int nums step Jump Game public


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


本题难度Hard。有2种算法分别是: 双指针法(推荐)和BFS(超时)

【注意】
本题对时间要求特别严格。
1、双指针法

【复杂度】
时间 O(N) 空间 O(1)

【思路】
用两个指针指出一块当前结点能跳的上下边界,然后再对这块区域遍历,找出这块区域能跳到的下一块区域的上下边界,每块区域都对应一步,直到上界超过终点时为之。

【代码】

public class Solution {
public int jump(int[] nums) {
//require
if(nums==null)
return 0;
int size=nums.length;
if(size<=1)
return 0;
//invariant
int step=0,preHigh=0,high=0,low=0;
while(high<size-1){
step++;
preHigh=high;
for(int i=low;i<=preHigh;i++)
high=Math.max(i+nums[i],high);
low=preHigh+1;
}
//ensure
return step;
}
}

2、BFS

【复杂度】
时间 O(N) 空间 O(N)

【思路】
广度优先搜索。可以解决该问题,不过超时。原因在于需要判断节点上一步是否已经搜索过(双指针法就不需要),耗费了时间。
实际上BFS更适合于图的路径搜索(而双指针法就解决不了)。

【代码】

public class Solution {
public int jump(int[] nums) {
//require
if(nums==null)
return 0;
int size=nums.length;
if(size<=1)
return 0;
//initialize queue
Set<Integer> set=new HashSet<Integer>();
Queue<Element> q=new LinkedList<Element>();
q.offer(new Element(0,0));
set.add(0);
//invariant
while(!q.isEmpty()){
Element e=q.poll();
if(e.index+nums[e.index]>=size-1)
return e.step+1;
for(int i=1;i<=nums[e.index]&&i+e.index<size;i++){
if(!set.contains(e.index+i)){
q.offer(new Element(e.step+1,e.index+i));
set.add(e.index+i);
}
}
}
return 0;
}

class Element{
int step=-1;
int index=-1;
public Element(int step,int index){
this.step=step;
this.index=index;
}
}
}

参考

​​[Leetcode] Jump Game 跳跃游戏​​


标签:II,return,index,int,nums,step,Jump,Game,public
From: https://blog.51cto.com/u_9208248/6033696

相关文章

  • mat 和IPIImage之间的转换
    Mat::operatorIplImageCreatestheIplImageheaderforthematrix.C++:Mat::operatorIplImage()constTheoperatorcreates theIplImageheaderforthematrixwi......
  • Awesome GameDev
    个人收集的与游戏开发相关的资源。 素材1. AI生成素材3D素材1. Devassets 设计像素1. 像素辅助利器:特效/粒子/流体/动画/节点编辑为一体SpriteMancer......
  • Good Bye 2022: 2023 is NEAR D. Koxia and Game(数据结构,图论,数学)
    题目链接:https://codeforces.com/problemset/problem/1770/D 大致题意:有三个数组,每个数组的长度为n,数组里面的每个数在(1-n)。现在,对于每一位上面的数,Mahiru可以去掉其中......
  • Consecutive sum II 1977
    ​​点击这里看题目链接ConsecutivesumII​​#include<stdio.h>#include<math.h>intmain(){unsignedlonglongm,n;scanf("%I64d",&n);while(n--){scan......
  • CF1780D Bit Guessing Game
    题目链接-https://codeforces.com/contest/1780/problem/D终端有一个需要猜的数\(x\),\(x\leq10^9\),每轮终端会给你返回一个数\(a\)表示\(x\)在二进制下有多少个......
  • CF1780E Bit Guessing Game
    题目链接-https://codeforces.com/contest/1780/problem/E给定一张无向图,其中有\(R-L+1\)个点,编号从\(L\)到\(R\),每两个点\(u\),\(v\)间连一条边,边权为\(gc......
  • CF1407D Discrete Centrifugal Jumps
    CF1407DDiscreteCentrifugalJumpsTJ蒟蒻的尸路:考场上看到这题果断DP(这是我第一次在考场自己想DP),然后$O(N^3)$原地爆炸。改成了$O(N^2)$还是炸,想到......
  • 硬件IIC主从机中断代码注释解析
    目录硬件IIC的主从中断在582的最新EVT中已支持。例程中已封装好中断处理过程,用户调用app_i2c时,初始化中需要配置回调函数即可。若需要串口打印日志,可以在工程预编译中增......
  • 通过 web deploy 发布 .net 网站到IIS
    安装webdeploy参考博客:https://www.jianshu.com/p/519f827b660b注意一点:webdeploy官网下载的中文版本是3.6的,会出现安装不上的情况。请使用英文版的,4.0版本......
  • IIS 配置 HTTPS
    1、导入证书a、开始->运行->MMC,打开MMC  b、文件->添加/删除管理单元c、双击证书,添加d、计算机用户      ......