LN : leetcode 646 Maximum Length of Pair Chain
lc 646 Maximum Length of Pair Chain
646 Maximum Length of Pair Chain
You are given n
pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d)
can follow another pair (a, b)
if and only if b < c
. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000].
DP Accepted
dp[i]表示前i+1个可以组成链的数对个数,先将数对根据第一个数进行从小到大的排序,可以得出动态转移方程为dp[i] = max(dp[i], pairs[i][0] > pairs[j][1]? dp[j] + 1 : dp[j])
。
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), cmp);
vector<int> dp(pairs.size(), 1);
for (int i = 0; i < pairs.size(); i++) {
for (int j = 0; j < i; j++) {
dp[i] = max(dp[i], pairs[i][0] > pairs[j][1]? dp[j] + 1 : dp[j]);
}
}
return dp[pairs.size()-1];
}
static bool cmp(vector<int>& a, vector<int>& b) {
return a[0] < b[0];
}
};
普遍方法 Accepted
根据每一对的第二个值将所有数对进行从小到大的排序,此时,循环遍历整个数组如果前一成链数对的第二个数小于当前遍历数对的第一个数,则又可以成链。这个方法很直观且简单。
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), cmp);
int maxlen = 1;
for (int i = 0, j = 1; j < pairs.size(); j++) {
if (pairs[i][1] < pairs[j][0]) {
maxlen++;
i = j;
}
}
return maxlen;
}
static bool cmp(vector<int>& a, vector<int>& b) {
return a[1] < b[1];
}
};
LN : leetcode 646 Maximum Length of Pair Chain的更多相关文章
- LC 646. Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 646. Maximum Length of Pair Chain(medium)
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 646. Maximum Length of Pair Chain 最长的链条长度
[抄题]: You are given n pairs of numbers. In every pair, the first number is always smaller than the s ...
- 646. Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- [LeetCode] Maximum Length of Pair Chain 链对的最大长度
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- [Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
随机推荐
- BestCoder Round #2 1001 TIANKENG’s restaurant
不得不说,bastcoder是个hack游戏啊.!. 题意:求最少要多少张椅子才干让全部来的客人有地方坐!! 就是一个区间的处理吧!!!和HDU 1556 我待水似流年.流年待我似水神似! ..! ...
- Kemans算法及其Python 实现
算法优缺点: 优点:容易实现缺点:可能收敛到局部最小值,在大规模数据集上收敛较慢使用数据类型:数值型数据 算法思想 k-means算法实际上就是通过计算不同样本间的距离来判断他们的相近关系的,相近的就 ...
- java反射(2)+Class.forName( )
在上一篇文章中说说java反射(1)我们已经了解了反射的基本概念以及它的原理,但是我们知道反射只能能用它来做些什么呢? 其实在我们很多已经很熟的框架中都有它的身影,例如Hibernate框架中的关联映 ...
- C 编程中fseek、ftell的用法总结
fseek 函数功能是将文件指针移动到指定的地方,因此可以通过fseek重置文件指针的位置.函数原型: int fseek(FILE *stream, long offset, int origi ...
- 使用scanf_s报错:0xC0000005: Access violation writing location 0x00000000
在vs2010中写了一行scanf("%s",name); 调式时 提示warning , 提示修改为scanf()使用可能会存在不安全,建议使用scanf_s() 但是我修改成s ...
- Infrastructure for container projects.
Linux Containers https://linuxcontainers.org/
- I.MX6 Android can-utils 移植
/******************************************************************* * I.MX6 Android can-utils 移植 * ...
- Ural2040:Palindromes and Super Abilities(离线&manecher算法)
Dima adds letters s1, …, sn one by one to the end of a word. After each letter, he asks Misha to tel ...
- 【USACO 2011】 道路和航线
[题目链接] 点击打开链接 [算法] SPFA + SLF / LLL 优化 [代码] #include<bits/stdc++.h> using namespace std; #defi ...
- Windows7下安装golang语言开发环境和revel框架
1.下载先去下载32位或64 golang window 安装包 并安装下载地址:https://www.golangtc.com/download 本人更改了安装地址为 D:\GO\Go 2. go ...