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 ...
随机推荐
- Halcon中数据的四舍五入、取整、有效数字以及和字符串之间的转换
*此代码可直接复制到HDevelop中运行 a:= 3.456 *取整.取最近的整数 int_a := int(a) //取整数部分,结果是3 round_a := round(a) //将输入元组转 ...
- php 框架选择
背景 很多初级php甚至中级php都会陷入框架选择困难症,要么必须使用什么框架,要么一定不使用什么框架,而对框架的选择带来的效益和负担的成本并不是很清晰 框架大概分为以下这些 1. 简单轻量:tp,c ...
- mysql 导入导出摘要
1.导入by数据文件. mysql>load data infile '文件路径' into table 表名 fields terminated by '字段分隔符' lines termin ...
- android的 Base64
byte[] key=Base64.decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4".getBytes(), Base64.DEFAULT); ...
- 《Just for Fun》---读后感
<Just for Fun>本书是Linux之父林纳斯自传,书名的意思是:只是为了好玩.主要是讲了林纳斯的人生经历,以及Linux的诞生过程.Linux从一个终端仿真器到一个世界瞩目的操作 ...
- 20155213 2016-2017-2 《Java程序设计》第八周学习总结
20155213 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章NIO与NIO2 NIO NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你 ...
- 03-vue-router
前端路由的实现原理 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Dbutils学习(介绍和入门)
一:Dbutils是什么?(当我们很难理解一个东西的官方解释的时候,就让我们记住它的作用) Dbutils:主要是封装了JDBC的代码,简化dao层的操作. 作用:帮助java程序 ...
- webuploader 文件上传总结
$(function() { /** * 初始化Web Uploader */ var uploader = WebUploader.create({ swf : '../mobile/Uploade ...
- JAVA开发常用计算机命令
系统常用命令 win+r > control (可进入控制面板,管理工具,服务) win+r > cmd > systeminfo (x86-based 指32位系统,x86-64 ...