leetcode 跳跃游戏系列
55. 跳跃游戏 能跳一个范围,贪心
class Solution {
public:
bool canJump(vector<int>& nums) {
int m = 0;
//每次拓展最右端点,大于nums.size()-1时返回
for(int i = 0; i < nums.size() ;i++){
if(i <= m) m = max(m, i + nums[i]);
if( m >= nums.size() - 1) return true;
}
return false;
}
};
45. 跳跃游戏 II 能跳一个范围,求跳跃数目,可用贪心
class Solution {
public:
int jump(vector<int>& nums) {
if(nums.size() == 1) return 0;
int m = 0;
int cnt = 0;
int i = 0;
//在一的基础上增加计数,每次到达上一次的最右端点时加一
while( i < nums.size()){
int newm = 0;
while(i <= m){
newm = max(i + nums[i], newm);
i++;
}
m = newm;
cnt++;
if(m >= nums.size() - 1) return cnt;
}
return -1;
}
};
1306. 跳跃游戏 III 跳两个点,dfs
class Solution {
public:
vector<int> vis;
bool canReach(vector<int>& arr, int start) {
vis.resize(arr.size(),0);
return dfs(arr,start);
}
bool dfs(vector<int>& arr, int start){
if(start < 0 || start >= arr.size() || vis[start]) return false;
if(arr[start] == 0) return true;
vis[start] = 1;
return dfs(arr, start + arr[start]) || dfs(arr,start - arr[start]);
}
};
1345. 跳跃游戏 IV 可调一些点,bfs,hash
class Solution {
public:
int minJumps(vector<int>& arr) {
int n = arr.size();
if(n == 1) return 0;
vector<int> vis(n,0);//用来记录是否访问过
int depth = 0;
//用hash表来记录相同的值
unordered_map<int,vector<int>> mp;
for(int i = 0; i < n; i++) mp[arr[i]].push_back(i);
queue<int> q;
q.push(0);vis[0] = 1;
while(q.size()){
int len = q.size();
while(len--){
int t = q.front(); q.pop();
if(t == n-1) return depth;
//右跳
if(t + 1 < n && !vis[t+1]) q.push(t+1),vis[t+1] = 1;
//左跳
if(t - 1 >= 0 && !vis[t-1]) q.push(t-1),vis[t-1] = 1;
//相同的值
if(mp.count(arr[t])){
for(int x:mp[arr[t]]){
if(x != t){
q.push(x);vis[x] = 1;
}
}
mp.erase(arr[t]);
}
}
depth++;
}
return -1;
}
};
1340. 跳跃游戏 V 可跳一些点,求最值问题:记忆化dfs,dp
class Solution {
public:
vector<int> dp;
int maxJumps(vector<int>& arr, int d) {
int ans = 0;
dp.resize(arr.size(),-1);
for(int i = 0; i < arr.size(); i++)
ans = max(ans, dfs(arr,i,d));
return ans;
}
//dfs记忆化
int dfs(vector<int>& arr, int start,int& d){
//递归出口
if(dp[start] != -1) return dp[start];
int res = 1;
//向左跳
for(int i = start - 1;i >= 0 && i >= start - d; i--){
if(arr[i] < arr[start])
res = max(res,dfs(arr,i,d) + 1);
else break;
}
//向右跳
for(int i = start + 1; i < arr.size() && i <= start + d; i++){
if(arr[i] < arr[start])
res = max(res,dfs(arr,i,d) + 1);
else break;
}
//记忆
dp[start] = res;
return res;
}
};
leetcode 跳跃游戏系列的更多相关文章
- Leetcode 跳跃游戏 II
题目链接:https://leetcode-cn.com/problems/jump-game-ii/ 题目大意: 略. 分析: 贪心 + DP. 代码如下: class Solution { pub ...
- LeetCode:跳跃游戏【55】
LeetCode:跳跃游戏[55] 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能够到达最后一个位置. 示例 1: 输入: ...
- [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 - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- LeetCode 45跳跃游戏&46全排列
原创公众号:bigsai,回复进群加入力扣打卡群. 昨日打卡:LeetCode 42字符串相乘&43通配符匹配 跳跃游戏 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中 ...
- 【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): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ
跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...
- Leetcode力扣45题 跳跃游戏 II
原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
随机推荐
- 数据结构——八大排序算法(java部分实现)
java基本排序算法 1.冒泡排序 顶顶基础的排序算法之一,每次排序通过两两比较选出最小值(之后每个算法都以从小到大排序举例)图片取自:[小不点的博客](Java的几种常见排序算法 - 小不点丶 - ...
- [cocos2d-x]捕鱼达人炮台射击角度的旋转实现
话不多说,先上图,下面是实现代码(在后面会具体讲解实现过程): //第一步:将炮台的坐标转换为世界坐标下的坐标点 CCPoint location = this->getParent()-> ...
- Scrapy爬虫框架快速入门
安装scrapy pip install scrapy -i https://pypi.douban.com/simple/ 安装过程可能遇到的问题 版本问题导致一些辅助库没有安装好,需要手动下载并安 ...
- day10-AOP-03
AOP-03 7.AOP-切入表达式 7.1切入表达式的具体使用 1.切入表达式的作用: 通过表达式的方式定义一个或多个具体的连接点. 2.语法细节: (1)切入表达式的语法格式: execution ...
- webSocket前端+nodejs后端简单案例多人在线聊天
一:下面是一个简单的案例,回车发送消息,多人在线聊天 1.前端代码 <!DOCTYPE html> <html> <head> <meta charset=& ...
- c++ 跑酷小游戏之用户体验plus
#undef UNICODE#undef _UNICODE#include <iostream>#include <iomanip>#include <string> ...
- 无需依赖Docker环境制作镜像
随着高版本的Kubernetes弃用Docker,企业也可以不依赖Docker环境了,但是DevOps通过Kubernetes部署的话,仍然需要制作镜像,那么在没有Docker环境的情况下如何制作呢? ...
- C-01\编译器和链接器以及真正的入口函数
编译器: 工具 编译器 路径 VC++6.0 CL.EXE(一段shell)只负责分析命令行参数,真正功能实现在C1.DLL.C1XX.DLL.C2.DLL C:\Program Files (x86 ...
- 重学SpringBoot. step5 再学SpringMVC
SpringMVC 参考:<深入浅出 SpringBoot 2.X> 虽然说的是SpringBoot,但把SpringMVC将的很好,正是SpringMVC应用到SpringBoot中非常 ...
- 2.3.pages.json文件的页面配置与全局配置
新建页面 # pages uni-app 通过 pages 节点配置应用由哪些页面组成,pages 节点接收一个数组,数组每个项都是一个对象,其属性值如下: 属性 类型 默认值 描述 path Str ...