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:

  1. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  6. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Codeforces 607A 动态规划

    A. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. 2018.06.29 洛谷P1505 [国家集训队]旅游(树链剖分)

    旅游 题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有 ...

  3. 2018.08.28 洛谷P3345 [ZJOI2015]幻想乡战略游戏(点分树)

    传送门 题目就是要求维护带权重心. 因此破题的关键点自然就是带权重心的性质. 这时发现直接找带权重心是O(n)的,考虑优化方案. 发现点分树的树高是logn级别的,并且对于以u为根的树,带权重心要么就 ...

  4. 21 Survival of Desert Life 沙漠生命的延续

    Survival of Desert Life 沙漠生命的延续 ① Some desert animals can survive the very strong summer heat and dr ...

  5. redis之单机和主从环境搭建

    单机环境搭建 官网http://redis.io/download下载xxx.tar.gz二进制压缩包,注意下载2.8+版本,2.8之前的版本之前从服务器不支持部分重复制,2.6之前的版本不支持set ...

  6. c++ 内联函数 (讲解的TM真好)

    1.  内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码如下: int max(int a, int b) {  return a > b ? a : b; } 为 ...

  7. Time&Patience

    “时间和耐心,是世间最强大的两个勇士.” “对未来越有信心,对今天越有耐心,坚持做正确的事.” 每日必做清单 每天六组俯卧撑    活在当下    接收脆弱    一万小时理论(罗马不是一天建成的,胖 ...

  8. saprk2 structed streaming

    netcat (windows) >nc -L -p 9999 import java.sql.Timestamp import org.apache.spark.sql.SparkSessio ...

  9. CSharp程序员学Android开发---1.初识AndriodIDE,掌握工具使用

    最近公司组织项目组成员开发一个Android项目的Demo,之前没有人有Andoid方面的开发经验,都是开发C#的. 虽说项目要求并不是很高,但是对于没有这方面经验的人来说,第一步是最困难的. 项目历 ...

  10. android应用搬家的实现

    android手机上的应用搬家功能,具体的介绍和原理参考: 系统目录及应用搬家的研究 应用搬家的实现 这里主要是作为一个补充,因为上面两篇文章虽然讲的挺好的,但是给出的例子不能直接运行,还是需要一些准 ...