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].
Approach #1: Greedy. [C++]
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), cmp);
int ans = 1;
int idx = 0;
for (int i = 1; i < pairs.size(); ++i) {
if (pairs[i][0] > pairs[idx][1]) {
ans++;
idx = i;
}
}
return ans;
}
static bool cmp(vector<int> a, vector<int> b) {
return a[1] < b[1];
}
};
Approach #2: DP. [Java]
class Solution {
public int findLongestChain(int[][] pairs) {
if (pairs == null || pairs.length == 0) return 0;
Arrays.sort(pairs, (a, b) -> (a[0] - b[0]));
int[] dp = new int[pairs.length];
Arrays.fill(dp, 1);
for (int i = 0; i < dp.length; ++i) {
for (int j = 0; j < i; ++j) {
dp[i] = Math.max(dp[i], pairs[i][0] > pairs[j][1] ? dp[j] + 1: dp[j]);
}
}
return dp[pairs.length - 1];
}
}
646. Maximum Length of Pair Chain的更多相关文章
- 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 number ...
- 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 ...
- 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 ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 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-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 ...
随机推荐
- iOS开发总结
最近在工作中使用Objective-C开发iOS客户端程序,它一方面和Server通讯,处理网络连接,收发报文,实现业务逻辑;另一方面为UI层提供各种业务API. 下面记录用到的相关知识点,困难, ...
- 765A Neverending competitions
A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...
- DNA甲基化检测服务
DNA甲基化检测服务 DNA甲基化是最早发现的基因表观修饰方式之一,真核生物中的甲基化仅发生于胞嘧啶,即在DNA甲基化转移酶(DNMTs)的作用下使CpG二核苷酸5'-端的胞嘧啶转变为5'-甲基胞嘧啶 ...
- postman get和post结合
- Linux多线程服务端编程 使用muduo C++网络库 学习笔记 日志log
代码来自陈硕开源代码库 muduo中 地址是https://github.com/chenshuo/muduo #pragma once #include <string> #define ...
- msys2 显示git branch
在.bashrc或.bash_profile中添加以下内容 function parse_git_branch () { git branch 2> /dev/null | sed -e '/^ ...
- IT 技术网站收集
## 脚本之家 http://www.jb51.net/ ## 菜鸟教程 http://www.runoob.com/ ## 编程中国 https://www.bccn.net/ ##
- 2018.10.17 NOIP模拟 管道(状压dp)
传送门 状压dp好题. 怎么今天道道题都有点东西啊 对于今天题目神仙出题人先膜为上策:%%%%DzYoAk_UoI%%%% 设f[i][j]f[i][j]f[i][j]表示选取点的状态集合为iii,当 ...
- 2018.06.29 NOIP模拟 繁星(前缀和)
繁星 [问题描述] 要过六一了,大川正在绞尽脑汁想送给小伙伴什么礼物呢.突然想起以前拍过一张夜空中的繁星的照片,这张照片已经被处理成黑白的,也就是说,每个像素只可能是两个颜色之一,白或黑.像素(x,y ...
- git分支删除
1.列出本地分支: git branch 2.删除本地分支: git branch -D BranchName 其中-D也可以是--delete,如: git branch --delete Bran ...