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 ...
随机推荐
- 挖掘更合适的MVP模式的架构设计
关于MVP,关于android,不得不说这篇博客已经来的非常晚了,这篇博客早就想写了,一直都在偷懒,就不给自己这么久的偷懒找借口了.尽管这篇文章po出来的比較晚.可是我所接触的程序猿一些朋友之 ...
- 提高比特率 有损 无损 Video-and-Audio-file-format-conversion 视频声音转码
3 Ways to Change Bitrate on MP3 Files https://www.online-tech-tips.com/software-reviews/change-bitra ...
- the JSON object must be str, not 'bytes'
{ "ErrorDump": "the JSON object must be str, not 'bytes'", "StatusCode" ...
- 求两个list的交集和并集
两个list的并集,只需去除重复元素即可: 将两个list放入同一个set中即可: 两个list的交集: 1将其中一个list放入set, 2循环另一个list,每次向set塞值, 3判断set的总数 ...
- I.MX6 PLL5 clock hakcing
/************************************************************************** * I.MX6 PLL5 clock hakci ...
- BZOJ 4815 数论
今年的重庆省选? 具体就是,对于每次修改,A[p,q]这个位置, 设d=gcd(p,q) ,则 gcd为d的每一个格子都会被修改,且他们之间有个不变的联系 A[p,q]/p/q==A[k,t]/k/ ...
- apache服务器本质
apache服务器本质上说是一个TCP socket服务,socket模型如下: 下面以worker MPM来说明apache代码中相应处理的位置在哪里: (以apache httpd 2.2.23版 ...
- snnu1111(子序列求和)
1111: 子序列求和 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 10 Solved: 2[Submit][Status][Web Board] [ ...
- Java-Runoob-高级教程-实例-数组:01. Java 实例 – 数组排序及元素查找
ylbtech-Java-Runoob-高级教程-实例-数组:01. Java 实例 – 数组排序及元素查找 1.返回顶部 1. Java 实例 - 数组排序及元素查找 Java 实例 以下实例演示 ...
- Html.PartialView(),html.Renderpartial,html.action.html.RenderAction 辅助方法
Html.Partial(), 返回HTML字符串 .参数为部分视图 html.RenderPartial(),不返回返回HTML字符串 ,直接输出响应流.参数为部分视图 一般用于主视图中已经存在了这 ...