博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode-Jump Game II
阅读量:4606 次
发布时间:2019-06-09

本文共 3074 字,大约阅读时间需要 10 分钟。

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

Have you met this question in a real interview?
 
Solution 1 (not the best solution):
We use an array to record the max reachable range of n steps into a list called stepMaxLen. We then go through every node i:
1. Find out the min steps to reach this step from the list stepMaxLen, i.e., minStep.
2. Check whether the max reachable length at i, i.e., A[i]+i, is larger than stepMaxLen[minStep+1], if yes, then update the stepMaxLen[minStep+1].
3. During update stepMaxLen, once at some step n, we reach the end (len-1), we then return n.
 
1 public class Solution { 2     public int jump(int[] A) { 3         if (A.length==0 || A.length==1) return 0; 4         int len = A.length; 5  6         int[] stepMaxLen = new int[len+1]; 7         Arrays.fill(stepMaxLen, 0); 8         stepMaxLen[1] = A[0]; 9         if (stepMaxLen[1]>=len-1) return 1;10         for (int i=1;i
stepMaxLen[minStep+1]){21 stepMaxLen[minStep+1] = i+A[i];22 if (i+A[i]>=len-1) return minStep+1;23 }24 }25 26 27 return -1; 28 29 }30 }

Solution 2 (better solution):

In solution 1, we actually do need to use loop to find out the minStep to reach node i. We only need to maintain the maxReachLen of current number of steps, i.e., the last node that can be reached by using current number of steps, and the max reachable length at current node. Once we reach a node that is beyong the maxReachLen of current step, it means that now the node can only be reached by using one more step, we then update the current number of steps and the maxReachLen of current step.

NOTE: the principle of solution 1 and solution 2 is actually the same.

NOTE_UPDATE: This is BFS solution. The idea here is maxStepReach define the boundary of current level (i.e., the node can be reached by X steps). Whenever, i exceeds the boundary, it enters next level. We keep recording maxReach, the maximum reachable nodes. Just when i == maxStepReach+1, the maxReach at that time is the boundary of the newly entered level.

1 public class Solution { 2     public int jump(int[] A) { 3         if (A.length==0 || A.length==1) return 0; 4         int len = A.length; 5          6         int step = 1; 7         int maxStepReach = A[0]; 8         int maxReach = A[0]; 9         if (maxStepReach>=len-1) return step;10         for (int i=1;i
maxStepReach){14 step++;15 maxStepReach = maxReach;16 if (maxStepReach>=len-1) return step;17 }18 19 if (maxReach

 

 

 

 

转载于:https://www.cnblogs.com/lishiblog/p/4102821.html

你可能感兴趣的文章
为什么很多语言选择在JVM上实现
查看>>
CSS Reset CSS Framework
查看>>
LeetCode算法扫题系列19
查看>>
nginx获取经过层层代理后的客户端真实IP(使用正则匹配)
查看>>
YII实现dropDownList 联动事件
查看>>
历届试题 高僧斗法
查看>>
linux命令系列 stat & touch
查看>>
[Tools] Webstorm Github的配置与使用
查看>>
鬼谷子绝学
查看>>
用Html5与Asp.net MVC上传多个文件
查看>>
Xcode中匹配的配置包的存放目录
查看>>
JavaScript将具有父子关系的原始数据格式化成树形结构数据(id,pid)
查看>>
MySQL服务使用
查看>>
C语言练手自己编写学生成绩管理系统
查看>>
20175204 张湲祯 2018-2019-2《Java程序设计》第二周学习总结
查看>>
How to lisp Lisp output a paragraph"500 Tph Dry Process Cement Plant Machinery Manufacturers"
查看>>
OpenMobile's Application Compatibility Layer (ACL)
查看>>
竞价广告系统-广告检索
查看>>
强哥PHP面向对象学习笔记
查看>>
[转]基于.NET平台常用的框架整理
查看>>