【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\) ...
随机推荐
- MySQL server has gone away(在执行sql的时候,莫名的报错)
原文:https://cenalulu.github.io/mysql/mysql-has-gone-away/ MySQL Server has gone away报错原因汇总分析 原因1. MyS ...
- Spark RDD----pyspark第四次作业
1.pyspark交互式编程 查看群里发的“data01.txt”数据集,该数据集包含了某大学计算机系的成绩,数据格式如下所示: Tom,DataBase,80 Tom,Algorithm,50 To ...
- Jenkins(1)- centos7.X下安装Jenkins
如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 下载安装包 wget http:/ ...
- XSS Challenge(2)
XSS Challenges http://xss-quiz.int21h.jp/ Stage #13 Hint:style attribute:要用到style属性,在style属性中有个expre ...
- 日志分析工具ELK(五)
八.Kibana实践 选择绝对时间和相对时间 搜索 还可以添加相关信息 自动刷新页面时间,也可以关闭 创建图像,可视化 编辑Markdown,创建一个值班联系表 值班联系表 保存 再创建一个饼图;查看 ...
- java并发中的Synchronized关键词
文章目录 为什么要同步 Synchronized关键词 Synchronized Instance Methods Synchronized Static Methods Synchronized B ...
- 李宏毅机器学习--PM2.5预测
一.说明 给定训练集train.csv,要求根据前9个小时的空气监测情况预测第10个小时的PM2.5含量. 训练集介绍: (1).CSV文件,包含台湾丰原地区240天的气象观测资料(取每个月前20天的 ...
- 【抓包工具】tcpdump
tcpdump - dump traffic on a network 根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析.它支 ...
- 老男孩教育每日一题-2017年3月29日-使用ifconfig取出网卡eth0的ip地址-看看你有多少方法...
方法1:sed命令 [root@oldboyedu ~]# ifconfig eth0 |sed -n '2p' |sed's#^.*addr:##g'|sed 's# B.*$##g' 10.0. ...
- fullpage.js禁止滚动
http://www.wenjiangs.com/doc/fullpage-method 转载于:https://www.cnblogs.com/hzz-/p/8268771.html