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 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
随机推荐
- [超详细] [效能工具]Typora+PicGo+Github免费图床快速搭建,提升技术文档输出效率
一.前言 在我们日常的学习和工作中,我们经常需要进行写作.尤其对于我们程序技术人员而言,工作中的技术方案文档或者接口文档等,都是经常需要用上的. 那么如果没有一个高效的工具,去帮助我们记录和创作,这将 ...
- [cocos2d-x]关于动画
声明一下:看见这篇文章总结的已经非常好了,没必要再去自己到处东翻西找了,链接:http://shahdza.blog.51cto.com/2410787/1546998 [唠叨] 基本动画制作需要用到 ...
- 云间玉兔,自出机抒,从零开始制作Web插件网页特效小兔子组件(小挂件widget),基于原生CSS/NPM
著意登楼瞻玉兔,何人张幕遮银阙?又到了一年一度的网页小挂件环节,以往我们都是集成别人开源的组件,但所谓熟读唐诗三百首,不会做诗也会吟,熟读了别人的东西,做几首打油诗也是可以的,但若不能自出机抒,却也成 ...
- LibreOJ 144. DFS 序 1
题面 给一棵有根树,这棵树由编号为 \(1\dots N\) 的 \(N\) 个结点组成.根结点的编号为 \(R\).每个结点都有一个权值,结点 \(i\) 的权值为 \(v_i\). 接下来有 \( ...
- 分布式协议与算法-Quorum NWR
1.强一致性与最终一致性 1.1强一致性 强一致性能保证写操作完成后,任何后续访问都能读到更新后的值:强一致性可以保证从库有与主库一致的数据.如果主库突然宕机,我们仍可以保证数据完整.但如果从库宕机或 ...
- Unreal学习笔记2-绘制简单三角形
目录 1. 概述 2. 详论 2.1. 代码实现 2.2. 解析:Component 2.3. 解析:材质 2.4. 解析:包围盒 2.5. 解析:Section 3. 其他 4. 参考 1. 概述 ...
- .NET Core 日志记录程序和常用日志记录框架
本文主要内容为.NET Core的日志记录程序和常使用的日志记录框架的简单使用 首先,打开VS2019新建一个ASP.NET Core Web Api项目,项目创建好后会有一个集成好的天气预报的类和控 ...
- fast planner总结
一.前端 kinodynamic A*算法动力学路径搜索 1.1 路径搜索的主要函数为kinodynamicAstar类的search函数 int KinodynamicAstar::search(E ...
- 合肥光源纵向震荡数据源相关PV
中控室束测界面上加上了BCM部分,看下面截图,可以发现里面的两张二维图像内容还是蛮丰富的,我想其数据很值得大家研究研究,现把相关PV公布给大家,感兴趣的可以通过这两幅图的数据计算出更多的物理量,比如每 ...
- Windows环境下FTP Server在局域网内的搭建
什么是FTP? File transfer protocol sreveris a computer software that facilitate the secure rxchange of f ...