[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.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then3steps to the last index.)
题意:问最小的跳跃次数
思路:这题是jump game的扩展。这题有两种解法:
第一种是动态规划,定义数组dp,dp[i]表示到达i处的最小跳跃次数。关键在确定dp[i]的值,我们可以遍历i之前的元素值,找到第一次能跳到或跳过i的下标,然后在其基础上加1即可。代码如下:
class Solution {
public:
int jump(int A[], int n)
{
vector<int> dp(n,);
if(n==) return ;
for(int i=;i<n;i++)
{
for(int j=;j<i;++j)
{
if(A[j]+j>=i)
{
dp[i]=dp[j]+;
break;
}
}
}
return dp[n-];
}
};
这种解法大的集合过不了。
第二种是贪心算法。其实个人觉得两种思路差不多,只是这种方法去掉了一些不必要的计算。遍历数组,找到当前能到达的最远距离curr,则,在这个距离中,不断的更新能到达的最远距离maxlen,遍历数组,若出了curr的范围,则计数器加1,并重新更新cur,至于i是如何出cur的范围不考虑。如:{3,3,1,1,4},

第一个3能达到的最远距离是curr,遍历数组,记下3到curr之间最新的最大距离maxlen,当i大于curr时,我们可以一步到达maxlen所在的位置,也就是说,我们不关心,从3到curr是如何到达的。参考了实验室小纸贴校外版的博客。代码如下:
class Solution {
public:
int jump(int A[], int n)
{
int curr=,maxLen=,count=;
for(int i=;i<n;++i)
{
if(i>curr)
{
curr=maxLen;
count++;
}
maxLen=max(maxLen,i+A[i]);
}
return count;
}
};
[Leetcode] jump game ii 跳跃游戏的更多相关文章
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)
https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- OSG-更新和回调
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- adb获取设备的序列号
用数据线连接手机, 打开开发者模式, 并赋予相关权限, 在CMD命令行输入: adb devices 第一个参数即为设备的序列号, 第二个参数device表示设备的状态是在线.
- 了解Python控制流语句——if语句
控制流 截止到现在,在我们所看过的程序中,总是有一系列语句从上到下精确排列,并交由 Python 忠实地执行.如果你想改变这一工作流程,应该怎么做?就像这样的情况:你需要程序作出一些决定,并依据不同的 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 孤荷凌寒自学python第八十四天搭建jTessBoxEditor来训练tesseract模块
孤荷凌寒自学python第八十四天搭建jTessBoxEditor来训练tesseract模块 (完整学习过程屏幕记录视频地址在文末) 由于本身tesseract模块针对普通的验证码图片的识别率并不高 ...
- Centos7下部署activeMQ消息队列服务
#1.下载activeMQlinux包 http://activemq.apache.org/activemq-5100-release.html 下载linux的activeMQ包 #2.使用X ...
- 重装win10后ubuntu引导项修复
问题描述:原来是在win7下装了ubuntu14的双系统,后台win7换win10,然后使用EasyBCD进行引导项修复时,不好使,报 error file: /boot/grub/i386-pc/n ...
- wf效能分析
听从了老师的建议我请教了其他的同学,修改了代码实现了功能四,以下是我的效能测试: 1.采用ptime.exe测试的3次截图 可以看到的是三次执行时间分别为:1.449秒:0.915秒:0.871秒,取 ...
- ueditor百度编辑器的赋值方法
示例: http://ueditor.baidu.com/website/onlinedemo.html 引用代码: window.UMEDITOR_HOME_URL = $CONFIG['domai ...
- Java中的基本数据类型包装类
在 java 中为什么会有基本数据类型的包装类? ①:基本数据类型之间的相互转换不是都可以制动转换的,而你强制转换又会出问题,比如String类型的转换为int类型的,那么jdk为了方便用户就提供了相 ...