leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏
问题描述
给定一个非负整数数组,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个位置。
示例 1:
输入: [2,3,1,1,4]
输出: true
解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置。
示例 2:
输入: [3,2,1,0,4]
输出: false
解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。
代码1(回溯法)
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
return Jump(nums,0,n);
}
bool Jump(vector<int>& nums,int position,int n)
{
if(position == n-1)return true;
else if(position > n-1)return false;
int furthermove = min(n-1,position+nums[position]);
//for(int i = position+1; i <= furthermove; i++)
for(int i = furthermove; i > position; --i)
{
if(Jump(nums,i,n))return true;
}
return false;
}
};
但是该方法在74/75测试用例超出时间限制。
代码2(剪枝回溯/自顶向下)
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
vector<int> flag;
flag.resize(n,-1);
flag[n-1] = 1;
return Jump(nums,0,n,flag);
}
bool Jump(vector<int>& nums,int position,int n,vector<int>& flag)
{
if(flag[position] == 1){
return true;
}
else if(flag[position] == 0) return false;
int furthermove = min(n-1,position+nums[position]);
//for(int i = position+1; i <= furthermove; i++)
for(int i = furthermove; i > position; --i)
{
if(Jump(nums,i,n,flag))
{
flag[i] = 1;
return true;
}
}
flag[position] = 0;
return false;
}
};
但遗憾的是该方法在74/75测试用例仍然超出时间限制。
代码3(自底向上)
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),i,j;
vector<int> flag;
flag.resize(n,-1);
flag[n-1] = 1;
for(i = n-2; i > -1; --i)
{
int furthermove = min(n-1,i + nums[i]);
for(j = i+1; j <= furthermove; ++j)
{
if(flag[j] == 1)
{
flag[i] = 1;
break;
}
}
}
return flag[0] == 1;
}
};
结果仍然不是很理想:
执行用时 :696 ms, 在所有 C++ 提交中击败了13.81%的用户
内存消耗 :15 MB, 在所有 C++ 提交中击败了5.04%的用户
代码4(贪心法)
用贪心来做,设置变量furthermove表示当前所能达到的最远的位置,那么状态转移方程为furthermove = max(furthermove, nums[i] + i),括号里的furthermove是上一步的最优解,因此是贪心。当到了某一个点 i>furthermove 的时候,说明已经走不到这一点了。
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),i,furthermove = nums[0];
for(i = 1; i < n && i<= furthermove; ++i)
{
// if(i <= furthermove)
furthermove = max(furthermove,i+nums[i]);
}
return furthermove>=n-1;
}
};
结果:
执行用时 :16 ms, 在所有 C++ 提交中击败了34.84%的用户
内存消耗 :14.7 MB, 在所有 C++ 提交中击败了5.04%的用户
45. 跳跃游戏 II
问题描述
给定一个非负整数数组,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
示例:
输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
说明:
假设你总是可以到达数组的最后一个位置。
问题分析
- 1、如果某一个作为 起跳点 的格子可以跳跃的距离是 3,那么表示后面 3 个格子都可以作为 起跳点;可以对每一个能作为 起跳点 的格子都尝试跳一次,把 能跳到最远的距离furthermove 不断更新。
- 2、首先从第0个元素开始,边界end是0,可以跳到的最远距离2,可以跳到1(i =1,furthermove就是4),也可以跳到2(i= 2,furthermove就是3),但是只有两个选择,贪心算法的要素就是获取当前阶段最优解;显然应该跳到1,此时边界end是4。遍历数组的时候,到了边界,我们就重新更新新的边界。
- 3、如果furthermove的值大于等于len-1,那么就可以一直跳到最后,就成功了。
代码
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size(),i,furthermove=0,end=0,step=0;
if(n == 1)return 0;
for(i = 0; i < n; i++)
{
furthermove = max(furthermove,nums[i]+i);
if(furthermove>=n-1)return ++step;
if(i == end)
{
end = furthermove;
++step;
}
}
return step;
}
};
问题分析:
执行用时 :12 ms, 在所有 C++ 提交中击败了73.08%的用户
内存消耗 :15.3 MB, 在所有 C++ 提交中击败了5.09%的用户
leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II的更多相关文章
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- [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 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ
跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...
- Unity 2D游戏开发教程之游戏中精灵的跳跃状态
Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...
随机推荐
- 安全防御之防xss、SQL注入、与CSRF攻击
XSS攻击 个人理解,项目中最普通的就是通过输入框表单,提交js代码,进行攻击例如在输入框中提交 <script>alert("我是xss攻击");</scrip ...
- react中使用Input表单双向绑定方法
input react 表单 input 密码框在谷歌浏览器下 会有黄色填充 官网的不太用,这个比较好用 type="password" autoComplete="ne ...
- 什么是SEO配置
SEO是什么 搜索引擎优化,又称为SEO,即Search Engine Optimization,它是一种通过分析搜索引擎的排名规律,了解各种搜索引擎怎样进行搜索.怎样抓取互联网页面.怎样确定特定关键 ...
- npm ERR! Error: EPERM: operation not permitted
转载于:https://blog.csdn.net/qq_36772866/article/details/86934950 win10 在npm install时报错 解决方案 删除node-mou ...
- RPA项目POC指南:概念、步骤与技巧
"为什么部署RPA前要进行POC?RPA不是开箱即用吗?" 其实,RPA的实施并非总是一帆风顺,"碰坑"在所难免. 据安永报告显示,30%至50%的初始RPA项 ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- Exponential family of distributions
目录 定义 性质 极大似然估计 最大熵 例子 Bernoulli 指数分布 正态分布 Choi H. I. Lecture 4: Exponential family of distributions ...
- CS5218DP转HDMI转接方案|CS5218说明|CS5218
Capstone CS5218是一款单端口HDMI/DVI电平移位器/中继器,具有重新定时功能.它支持交流和直流耦合信号高达3.0-Gbps的操作与可编程均衡和抖动清洗.它包括2路双模DP电缆适配器寄 ...
- Java网络编程Demo,使用TCP 实现简单群聊功能Groupchat,创建一个服务端,使多个客户端都能收到消息
效果图: 开启服务端 客户端一 客户端二 客户端三 实现代码: 客户端类 import java.io.IOException; import java.net.ServerSocket; impor ...