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

dp的思想

class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
int len = pairs.size();
if (len == ){
return ;
}
int res = ;
sort(pairs.begin(), pairs.end(), cmp);
vector<int> dp(len, );
for (int i = ; i < len; i++){
for (int j = ; j < i; j++){
if (pairs[i][] > pairs[j][]){
dp[i] = max(dp[i], dp[j] + );
}
}
}
return dp[len - ];
}
private:
static bool cmp(vector<int>& a, vector<int>&b){
return a[] < b[];
}
};

646. Maximum Length of Pair Chain(medium)的更多相关文章

  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. 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)

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

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

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

  7. LeetCode Maximum Length of Pair Chain

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

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

  9. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

随机推荐

  1. .Net File类的操作

    //File类的常用操作方法 //File类的常用操作方法 //File.Exists //检测是否存在该文件 Exists //File.Create //创建一个文件 Create //File. ...

  2. "每日一道面试题".net托管堆是否会存在内存泄漏的情况

    首先说答案:会 所谓的内存泄漏,就是指内存空间上产生了不再被实际使用却又无非被分配的对象.严格意义上来说,在.net中经常会遇到内存泄漏的情况,因为托管堆内的对象不再被使用时,需要等待下一次GC才会被 ...

  3. 【Unity】微软的一款依赖注入组件

    前言 前面学习了autofac这个依赖注入组件,本来是打算写在一起的,因为这个组件没打算像autofac一样详细的写,只是写下以前自己鼓捣玩搭建框架然后使用的一个依赖注入组件,并且也是进行了封装使用. ...

  4. 永不重复的id生成器

    目录 (1)需要导入的包 (2)IdGenerator类 (3)使用举例 (1)需要导入的包 主要用在格式化日FastDateFormat.getInstance("yyyyMMddHHmm ...

  5. Linux CentOS设置定时重启:crontab

    上一篇介绍了 开机自启动chkconfig命令  https://www.cnblogs.com/prefectjava/p/9399470.html 本篇介绍 crontab 设置定时任务,并且把 ...

  6. console对象探究

    作为一个前端,console.log()可能是你最常用的方法,打印打印再打印,但是其实console对象上有用的方法有很多,来,各位看官上眼 分类输出 厌倦了 console.log 单调的输出?欢迎 ...

  7. #WEB安全基础 : HTML/CSS | 0x10.1更多表单

    来认识更多的表单吧,增加知识面 我只创建了一个index.html帮助你认识它们 以下是代码 <!DOCTYPE html> <html> <head> <m ...

  8. C#设置电脑时间帮助类

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. 测者的测试技术手册:AI的自动化单元测试

    测者的测试技术手册:AI的自动化单元测试 谈新技术:AI的自动化单元测试    

  10. 批量删除MSSQL 中主外键约束

    转自: http://www.maomao365.com/?p=813 在制作 MSSQL同步工具的时候,发现由于主外键的约束,导致数据同步异常,所有我们需要把 读数据库里面的主外键约束,进行批量删除 ...