随笔分类 -  基础算法刷题记录

摘要:动规问题,如果直接使用滚动数组不开空间会效率更高,所有效率可以提升百分之40左右; 状态转移方程: dp[i]=min(dp[i-1],dp[i-2])+cost[i]; class Solution { public: int minCostClimbingStairs(vector<int>& 阅读全文
posted @ 2020-12-21 09:03 暮云林凌 阅读(64) 评论(0) 推荐(0)
摘要:基本思想: 典型的平面最短举例点对得问题,专题总结过; 关键点: 无; #include<iostream> #include<vector> #include<algorithm> using namespace std; struct point { double x; double y; }; 阅读全文
posted @ 2020-04-09 12:26 暮云林凌 阅读(185) 评论(0) 推荐(0)
摘要:基本思想: 第一次见到01背包中的最小值背包问题,即相同容量下装满所能有的最小值,如果没法装满,则dp[m]=MAX; 具体思想和最大值01背包思想有出入; 最大值背包思想是从0开始,并且从后向前更新; 而最小值背包思想dp[0]作为边界,剩下初始化为最大值,遍历更新方式和最大值背包问题方式相同; 阅读全文
posted @ 2020-04-04 20:49 暮云林凌 阅读(446) 评论(0) 推荐(0)
摘要:基本思想: 总结过; 关键点: 无; #include<iostream> #include<vector> #include<algorithm> using namespace std; const int maxn = 10000; int c, n; int dp[maxn]; int w[ 阅读全文
posted @ 2020-04-04 11:42 暮云林凌 阅读(178) 评论(0) 推荐(0)
摘要:基本思想: 还是要注意索引问题; 关键点: 无; #include<iostream> #include<string> #include<algorithm> #include<vector> using namespace std; vector<vector<int>> dp; int mai 阅读全文
posted @ 2020-04-02 12:33 暮云林凌 阅读(137) 评论(0) 推荐(0)
摘要:基本思想: 总结过,不赘述; 关键点: 无; #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; const int maxn = 110; int dp[maxn 阅读全文
posted @ 2020-04-02 12:06 暮云林凌 阅读(161) 评论(0) 推荐(0)
摘要:基本思想: 思想已经领悟,但是卡在了一个最后的寻找方法上,卡了1个用例; 最后寻找的应该是中间节点,而不是遍历,找后续的最长序列。否则会遇到这种问题: 1 2 3 3 2 1; 使用找中间节点则是3是中间节点,总和为5,序列为1,2,3,2,1; 而如果用自己最初的思想,则是总和为6,序列为1,2, 阅读全文
posted @ 2020-04-01 20:25 暮云林凌 阅读(192) 评论(0) 推荐(0)
摘要:基本思想: 无; 关键点: 无; #include<iostream> #include<vector> #include<algorithm> using namespace std; const int maxn = 10100; int n; int d[maxn]; int dp[maxn] 阅读全文
posted @ 2020-04-01 19:21 暮云林凌 阅读(202) 评论(0) 推荐(0)
摘要:基本思想: 打表、递归、dp都可以; 关键点: 无; #include<iostream> using namespace std; const int maxn = 25; int n; int dp[maxn]; int main() { while (cin >> n) { dp[1] = 1 阅读全文
posted @ 2020-04-01 19:00 暮云林凌 阅读(159) 评论(0) 推荐(0)
摘要:基本思想: 隐藏下的最大连续数和的问题,不容易看出来,从矩阵的性质中才可能看出来。 自己最初是想用二维DP来标识0-x,0-y的矩阵,但是发现不能表示非0起点的最大矩阵和; 一些大佬的思想是通过多行合并来计算,例如1-3行合并,则是找3*n的最大子矩阵,可以通过一次最长连续和求解,这个第一次见; 关 阅读全文
posted @ 2020-04-01 18:02 暮云林凌 阅读(165) 评论(0) 推荐(0)
摘要:基本思想: 无; 关键点: 无; #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn = 1000100; ll dp[maxn]; ll d[maxn]; 阅读全文
posted @ 2020-04-01 11:21 暮云林凌 阅读(175) 评论(0) 推荐(0)
摘要:基本思想: 只需要缕清关系即可; 对于第i个楼梯,可以得知有两种情况: 1.i-1 和 i两部跨; 2.i一步跨; 所以有: dp[i]=dp[i-1]+dp[i-2]; dp初始化的时候直接初始化dp[0],dp[1]即可; 关键点:无; #include<iostream> #include<a 阅读全文
posted @ 2020-04-01 11:14 暮云林凌 阅读(196) 评论(0) 推荐(0)
摘要:基本思想: 最长不下降子序列的改良版; 只需要更改状态转移方程即可,dp数组内存储的是最大子序列和; dp[i] = max(dp[i], dp[j] + num[j]) 状态转移方程如上所示,且j的取值范围为0~i-1; 关键点: 无; #include<iostream> #include<st 阅读全文
posted @ 2020-04-01 10:47 暮云林凌 阅读(120) 评论(0) 推荐(0)
摘要:基本思想: 最长不下降字串问题,注意dp数组的思想; 关键点: 无; #include<iostream> #include<vector> #include<algorithm> using namespace std; const int maxn = 25; int n; int d[maxn 阅读全文
posted @ 2020-03-31 13:42 暮云林凌 阅读(175) 评论(0) 推荐(0)
摘要:基本思想: 首先的考虑是采用大数下的迪杰斯特拉来计算,比较繁琐,而且有多重边的情况; 其次可以采用稀疏图下的最小生成树算法来做; 原因: 1.给出的边集天然递增; 2.边满足后续边总大于之前边的和,所以天然可以保证贪心构造生成树,即为最短路径; 值得注意的是老生常谈的取模问题: 方法一: #incl 阅读全文
posted @ 2020-03-30 16:19 暮云林凌 阅读(157) 评论(0) 推荐(0)
摘要:基本思想: 解法1: 抓住一个关键点:只可以从阵营1到阵营2; 如果是1阵营,则可以到达1或者2阵营; 如果是2阵营,则只可以到达2阵营; 所以在此基础上,进行迪杰斯特拉的优化问题; 解法2: 看一位大佬所给出思想,之前自己想删边,结果失败了。 由于只能从1阵营到2阵营,所以只需要删除从2阵营到1阵 阅读全文
posted @ 2020-03-30 14:19 暮云林凌 阅读(205) 评论(0) 推荐(0)
摘要:基本思想: 最初想到了聚类问题上,通过并查集聚类生成新的结点,但是极其麻烦; 所以还是按照示例所给的方法,直接将路径权值转换为0,从而直接一次最小生成树来做; 关键点: 无; #include<iostream> #include<vector> #include<string> using nam 阅读全文
posted @ 2020-03-30 13:36 暮云林凌 阅读(153) 评论(0) 推荐(0)
摘要:基本思想: 最简单的prim即可解决; 关键点; 无; #include<iostream> #include<vector> #include<string> using namespace std; const int maxn = 1010; const int INF = 10000000; 阅读全文
posted @ 2020-03-30 13:11 暮云林凌 阅读(133) 评论(0) 推荐(0)
摘要:基本思想: 感觉以后要注意一下,最小生成树里面有几个坑: 1.自环问题; 2.两节点间拥有多重路径; 关键点: Prim基础版本无需考虑自环问题; #include<iostream> #include<stdlib.h> #include<vector> #include<string> usin 阅读全文
posted @ 2020-03-30 13:01 暮云林凌 阅读(124) 评论(0) 推荐(0)
摘要:基本思想: 本质上是利用一次稠密图Prim算法解决,不难; 关键点: 无; #include<iostream> #include<vector> #include<string> #include<algorithm> #include<cmath> using namespace std; co 阅读全文
posted @ 2020-03-28 21:53 暮云林凌 阅读(160) 评论(0) 推荐(0)