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 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
随机推荐
- MySql树形结构(多级菜单)查询设计方案
背景 又很久没更新了,很幸运地新冠引发了严重的上呼吸道感染,大家羊过后注意休息和防护 工作中(尤其是传统项目中)经常遇到这种需要,就是树形结构的查询(多级查询),常见的场景有:组织架构(用户部门)查询 ...
- Go+beego接入OSS上传
路由 f, h, err := c.GetFile("uploadFile") if err != nil { logx.Error("getfile err " ...
- Codeforces Round #845 (Div. 2) and ByteRace 2023 A-D
Codeforces Round #845 (Div. 2) and ByteRace 2023 A-D A. Everybody Likes Good Arrays! 题意:对给定数组进行操作:删除 ...
- Unity模块嵌入到Android中
嗨,大家好,小黑在沉寂了6个月之后,终于要继续写一篇博客了. 先吐槽一波上家公司PHD&&OMS,不吐不快.上家公司的小黑,每天不是在弄UIWidgets,就是再弄UIWidgets, ...
- C++中进程间相互通信的方法
详细资料查看https://www.cnblogs.com/swunield/articles/3893250.html 在我看来主要分方式就是 1.共享内存 2.postmessage发送出去 3. ...
- JAVA虚拟机07-垃圾回收-分代收集理论和垃圾收集算法
1.分代收集理论 1.1分代收集理论假说 1.2分代收集理论奠定的垃圾收集器原则 1.3基于分代收集理论的内存划分-跨代引用假说 2.垃圾回收 3.垃圾收集算法 3.1标记-清除算法 3.2标记-复制 ...
- Linux06-常用命令 日期 日历 查找 过滤
1.date 日期 2.cal 日历 3.find 查找 4.locate 查找 5.grep 过滤 1.时间日期类 date指令-显示当前日期 1)指令: date (功能描述:显示当前时间) ...
- Vue框架-03:JS的几种循环方式,Key值的解释,数组/对象的检测与更新,input事件,v-model数据双向绑定,过滤案例,事件修饰符,按键修饰符,表单控制
目录 前端开发之Vue框架 一.JS循环的几种方式 1.v-for可循环的变量 2.js的循环方式 二.Key值的解释 三.数组.对象的检测与更新 四.input事件 五.v-model双向数据绑定 ...
- 国际财务系统基于ShardingSphere的数据分片和一主多从实践
作者:京东物流 张广治 1 背景 传统的将数据集中存储至单一数据节点的解决方案,在性能和可用性方面已经难于满足海量数据的场景,系统最大的瓶颈在于单个节点读写性能,许多的资源受到单机的限制,例如连接数. ...
- Redis 源码解读之 Rehash 的调用时机
Redis 源码解读之 Rehash 的调用时机 背景和问题 本文想要解决的问题 什么时机触发 Rehash 操作? 什么时机实际执行 Rehash 函数? 结论 什么时机触发 Rehash 操作? ...