【Leetcode】1340. Jump Game V 【动态规划/记忆性搜索】
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jump-game-v
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
-----------------------------------------------------------------------------------------------------------------
【难点分析】
某位置可访问的最多节点数与周围i-x ~ i+x的节点有关,容易想到用动态规划的方法来做。然而一个节点能到达的节点数不仅与左半边节点相关,也与右半边节点相关,所以不能简单的从0开始遍历整个数组。
【解决方案】:
方案一:
对可以访问的节点数从1开始进行遍历,即dp[i][j] 表示第i个节点是否可以访问j个节点,直到左右dp[i][j]都为false时循环结束。对每一个dp[i][j],寻找他的左边及右边满足条件的节点中可以使他为true的节点,一旦发现后便退出循环。
class Solution {
public:
int maxJumps(vector<int>& arr, int d) {
int n = arr.size();
vector<vector<bool>> dp(n, vector<bool>(n+, false));
for(int i = ; i < n; ++i)
dp[i][] = true;
for(int jp = ; jp <= n; jp++) {
bool flag = false;
for(int i = ; i < n; ++i) {
if(!dp[i][jp-]) continue;
for(int j = i-; j >= && i-j <= d && arr[j] < arr[i]; j--) {
if(dp[j][jp-]) {
dp[i][jp] = true;
//cout << i << ", " << jp << endl;
flag = true;
break;
}
}
if(dp[i][jp]) continue;
for(int j = i+; j < n && j-i <= d && arr[j] < arr[i]; j++) {
if(dp[j][jp-]) {
dp[i][jp] = true;
//cout << i << ", " << jp << endl;
flag = true;
break;
}
}
}
if(!flag) return jp;
}
return n+;
}
};
时间复杂度:O(d*n^2)
方案二.
对于需要根据未知状态来确定当前状态值的动态规划问题,可以用记忆化搜索方法来解决,即如果dp[i] = func(dp[j]), 若dp[j]还未求出,则先去求dp[j]。这种方法要注意的是是否存在循环的状况,即类似于dp[i] = func(dp[j]), dp[j] = func(dp[k]), dp[k] = func(dp[i])。
因为本题中如果dp[i]需要由dp[j]来求得,则arr[j]必小于arr[i],dp[j]不可能由dp[i]直接或间接决定。
class Solution {
private:
vector<int> jmp;
public:
void dfs(vector<int>& arr, int id, int d) {
if(jmp[id] != ) return;
jmp[id] = ;
for(int t = ; t <= d && id+t < arr.size() && arr[id+t] < arr[id]; t++) {
dfs(arr, id+t, d);
jmp[id] = max(jmp[id], jmp[id+t]+);
}
for(int t = ; t <= d && id-t >= && arr[id-t] < arr[id]; t++) {
dfs(arr, id-t, d);
jmp[id] = max(jmp[id], jmp[id-t]+);
}
}
int maxJumps(vector<int>& arr, int d) {
int n = arr.size();
jmp.resize(n, );
for(int i = ; i < n; ++i) {
dfs(arr, i, d);
}
return *max_element(jmp.begin(), jmp.end());
}
};
时间复杂度: O(n*d) //每一个节点只需计算一次,需要与i-d ~ i+d的节点进行对比
Leetcode相似题目还有:135
【Leetcode】1340. Jump Game V 【动态规划/记忆性搜索】的更多相关文章
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)
描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...
- [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)
题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...
- Poj-P1088题解【动态规划/记忆化搜索】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...
- UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 滑雪---poj1088(动态规划+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 有两种方法 一是按数值大小进行排序,然后按从小到大进行dp即可: #include <iostream> #incl ...
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Bone Collector(01背包+记忆化搜索)
Bone Collector Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- 【UVA11324】 The Largest Clique (Tarjan+topsort/记忆化搜索)
UVA11324 The Largest Clique 题目描述 给你一张有向图 \(G\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) ...
随机推荐
- 2019-2020-1 20199328《Linux内核原理与分析》第十二周作业
缓冲区溢出 2019/12/4 11:33:45 首先是安装一些用于编译的32位C程序e148 $ sudo apt-get update $ sudo apt-get install -y lib3 ...
- Linux系统管理第一二三四章 系统管理 目录和文件管理 安装及管理程序 账号管理
命令 功能 序号 第一章 cd 切换目录 1 stat 查看文件状态信息 2 cp 复制 -f -i -p -r 3 du 统计磁盘的大小 4 find 精细查找文件和目录 5 help 帮助 ...
- Java阻塞队列四组API介绍
Java阻塞队列四组API介绍 通过前面几篇文章的学习,我们已经知道了Java中的队列分为阻塞队列和非阻塞队列以及常用的七个阻塞队列.如下图: 本文来源:凯哥Java(kaigejava)讲解Java ...
- 【高并发】由InterruptedException异常引发的思考
写在前面 InterruptedException异常可能没你想的那么简单! 前言 当我们在调用Java对象的wait()方法或者线程的sleep()方法时,需要捕获并处理InterruptedExc ...
- centos 7.0运行docker出现内核报错解决方法
目前我这里docker是运行在centos 7.0系统里,使用1.5版本docker,最近一台服务器总是不定期死机,通过查看日志发现属于内核bug导致,报错信息如下 1 2 3 4 5 6 7 8 9 ...
- 提高Web服务器并发响应的经历
1 前言 ---------- 最近一直在维护一个线上运行的旧系统,系统本身的问题很多,然而又有大量客户准备试用.之前一直存有侥幸心理,希望系统能神奇的顶过这段时间,但这个蜗牛般的系统残忍的告诉我们- ...
- phpsocket.io
https://github.com/walkor/phpsocket.io phpsocket.io A server side alternative implementation of sock ...
- PHP版DES算法加密数据(3DES)另附openssl_encrypt版本
PHP版DES算法加密数据(3DES) 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 <?php /** * Created by PhpStorm. * ...
- 虚拟 IP 设为静态 IP
一:虚拟机设置桥接模式 1.进入虚拟机设置中将网络适配器设置成桥接模式 2.编辑--虚拟网络编辑器--选择桥接 二:将虚拟IP设置成静态IP (1)方案一:进入虚拟机系统 System 设置 (2)方 ...
- CentOS7 安装boost
(1)到这个网址www.boost.org下载相应的代码包,我下载的是目前最新的版本boost_1_59_0.tar.bz2 (2)进入目录执行解压操作:tar -jxvf boost_1_59_0. ...