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 跳跃游戏系列的更多相关文章

  1. Leetcode 跳跃游戏 II

    题目链接:https://leetcode-cn.com/problems/jump-game-ii/ 题目大意: 略. 分析: 贪心 + DP. 代码如下: class Solution { pub ...

  2. LeetCode:跳跃游戏【55】

    LeetCode:跳跃游戏[55] 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.判断你是否能够到达最后一个位置. 示例 1: 输入: ...

  3. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. 力扣Leetcode 45. 跳跃游戏 II - 贪心思想

    这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...

  5. LeetCode 45跳跃游戏&46全排列

    原创公众号:bigsai,回复进群加入力扣打卡群. 昨日打卡:LeetCode 42字符串相乘&43通配符匹配 跳跃游戏 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中 ...

  6. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  8. [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ

    跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...

  9. Leetcode力扣45题 跳跃游戏 II

    原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...

  10. LeetCode 45. 跳跃游戏 II | Python

    45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...

随机推荐

  1. python进阶之路7 数据类型的内置方法

    内容回顾 while 循环补充说明 1.死循环 2.while循环嵌套和全局标志位 for循环 1.for 变量名 in 待遍历数据 for循环体代码 2.for 也可以与break continue ...

  2. XCTF-web新手区

    前言 刷题平台:攻防世界 web简介 WEB是CTF竞赛的主要题型,题目涉及到许多常见的WEB漏洞,诸如XSS.文件包含.代码执行.上传漏洞.SQL注入.还有一些简单的关于网络基础知识的考察,例如返回 ...

  3. 创建型模式 - 原型模式Prototype

    孩子生来没娘的NT审核机制,又开始说我涉及到广告了,我涉及到什么广告了?我接着发. 学习而来,代码是自己敲的.也有些自己的理解在里边,有问题希望大家指出. 所属:创建型模式  原型模式 一般会和 工场 ...

  4. MySQL-SQL语法、字段类型

    1.字符编码与配置文件 1.\s:查看当前MySQL相关信息:当前用户.版本.编码.端口号. """ Server characterset.Db characterse ...

  5. react无效渲染优化--工具篇

    壹 ❀ 引 本文属于我在公司的一篇技术分享文章,它在我之前 React性能优化,六个小技巧教你减少组件无效渲染一文的基础上进行了拓展,增加了工具篇以及部分更详细的解释,所以内容上会存在部分重复,以下是 ...

  6. C-03\浮点数转换与编码和补码

    工程生成文件格式了解(常用) 工具 文件 作用 vc++6.0 .dsw 最高级别的配置文件,记录了整个工作空间的配置信息,是一个纯文本的文件,创建新项目时自动生成 vc++6.0 .dsp 配置文件 ...

  7. 学习Java Day25

    今天学习了类路径和如何设置类路径 其中需要创建JAR后面才会介绍如何创建JAR,类路径可以节省空间并改善性能,设置类路径可以用-classpath选项指定路径

  8. 对Jim博士质疑的质疑

    ​ 我只是中科大一个本科生,不像Jim博士那样顶了博士的帽子.去年他上头条的时候评论了他的一篇文章. 看了他的一些文章,感觉他对国内科研现状以及和美西方的差距非常了解,并且做了大量的调研,站在国家的立 ...

  9. MVC3三层架构

    以上部分来自黑马

  10. ctfshow_web入门 反序列化(254~266)

    要是没接触过的师傅们,可以先看看这个 web 254 这个题没有考什么,get方式传入payload即可,这里xxxxxx,就是6gex而已 payload: ?username=xxxxxx& ...