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. DVWA靶场实战(五)——File Upload

    DVWA靶场实战(五) 五.File Upload: 1.漏洞原理: File Upload中文名叫做文件上传,文件上传漏洞是指用户上传了一个可执行脚本文件(php.jsp.xml.cer等文件),而 ...

  2. centos7.6安装本地yum源

    centos7.6安装本地yum源 前言:文章内容可能会因环境不同而有所差异,所谓集思广益说不定灵感就来了呢; 文章初衷旨在交流学习.记录个人成长,如果能帮助到您,那就点个赞噢. 环境说明: 1.本实 ...

  3. 解决 requests cookies 为空的坑

    转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/5d14f0d7.html 你好,我是测试蔡坨坨. 我们在做接口自动化测试的时候,一般会通过调用登录接口来获取cookies. ...

  4. SpringCloud NetFlix学习

    SpringCloud NetFlix 遇到记录不完全的可以看看这个人的博客 学相伴SpringCloud 微服务架构的4个核心问题? 服务很多,客户端该怎么访问? 负载均衡.反向代理,用户请求的永远 ...

  5. mysql 5.7安装教程及密码设置

    1.安装网址 https://dev.mysql.com/downloads/mysql/ 2.点击 Archives 3.切换版本,之前安装过新版本出现过很多问题,为了方便学习,所以选择了5.7这个 ...

  6. JSP第二次作业

    1.p39   实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23) 1 <%@ page language="java" import ...

  7. Html5 canvas创意特效合集

    Canvas就像一块画布,我们可以通过调用脚本在Canvas上绘制任意形状,甚至是制作动画.本文就是收集了很多非常富有创意的一些canvas动画特效例子,这些例子都非常适合大家学习.更多源码可在在这里 ...

  8. el-transfer 数据量过大加载慢卡顿解决办法:el-transfer虚拟滚动懒加载的实现

    参考链接 1)https://github.com/GreenHandLittleWhite/blog/issues/152)https://github.com/GreenHandLittleWhi ...

  9. 关于联想对Jim博士的质疑

    对Jim博士质疑的质疑 因为关注司马南,从他的空间里看到Jim博士和其龃龉,大致看了Jim博士头条里的文章,因为看到自己常用的EPICS,上午匆忙就写了上面的文. Jim博士是去年在头条上看到的,因为 ...

  10. 安卓逆向 IDA 动态调试 案例1

    adb forward tcp:23946 tcp:23946 adb devices adb shell su cd /data/local/tmp ./android_server adb she ...