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:

  1. Input: [[1,2], [2,3], [3,4]]
  2. Output: 2
  3. Explanation: The longest chain is [1,2] -> [3,4]

Note:

  1. The number of given pairs will be in the range [1, 1000].

dp的思想

  1. class Solution {
  2. public:
  3. int findLongestChain(vector<vector<int>>& pairs) {
  4. int len = pairs.size();
  5. if (len == ){
  6. return ;
  7. }
  8. int res = ;
  9. sort(pairs.begin(), pairs.end(), cmp);
  10. vector<int> dp(len, );
  11. for (int i = ; i < len; i++){
  12. for (int j = ; j < i; j++){
  13. if (pairs[i][] > pairs[j][]){
  14. dp[i] = max(dp[i], dp[j] + );
  15. }
  16. }
  17. }
  18. return dp[len - ];
  19. }
  20. private:
  21. static bool cmp(vector<int>& a, vector<int>&b){
  22. return a[] < b[];
  23. }
  24. };

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. 痞子衡嵌入式:一表全搜罗常见低功耗广域物联网协议(NB-IoT/eMTC/LoRa/SigFox...)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是低功耗广域物联网协议. 上一篇痞子衡给大家搜罗了短距离无线通信协议,它是物联网的基础,但它的应用距离比较短,对于长距离的物联网应用鞭长莫 ...

  2. 【Zabbix】zabbix设置邮件报警

    目录 Zabbix设置邮件报警 1.安装sendmail或postfix 2.安装邮件发送工具mailx . 3.配置mail 4. 测试邮件发送 5.编写邮件发送脚本sendmail.sh 6.设置 ...

  3. .Net语言 APP开发平台——Smobiler学习日志:获取或存储图像路径设置

    ResourcePath属性 一.属性介绍 获取或设置图像存储路径,默认设置为“image”,表示的ResourcePath是在程序运行路径下的Image文件夹(bin\Debug\Image): 该 ...

  4. Sql 语句拼接 多条件分页查询

    Create PROCEDURE [dbo].[Proc_B2B_GetBatchMainPaging] @StationNo AS varchar() , --m @StationName AS v ...

  5. C#基础-九九乘法表和冒泡排序

    //乘法表 ; i < ; i++)//行 { ; j < ; j++)//列 { if (j <= i) { Console.Write("{0}*{1}={2}\t&q ...

  6. _C#发送邮箱

    public ActionResult lead() { SendEmail("邮箱号", "吃饭么?", "你要吃什么啊"); retur ...

  7. js实现多个倒计时并行 js拼团倒计时

    本文是对类似于拼团,多个商品每个都有各自的js倒计时,一开始接到接到这个需求也是头疼了一阵子,如果是在商品列表少的时候完全就可以写成死的,固定的变量,写几个定时器就ok了, 但是这次数据是活的,看一些 ...

  8. js 数组随机洗牌

    //先定义一个某数值范围内的随机数 function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1) + ...

  9. Android为TV端助力 进制互相转换

    byte转换为16进制 public static String GetByte2Str(byte b) { byte[] buff = new byte[2]; buff[0] = mHex[(b ...

  10. spring学习总结——装配Bean学习三(xml装配bean)

    通过XML装配bean Spring现在有了强大的自动化配置和基于Java的配置,XML不应该再是你的第一选择了.不过,鉴于已经存在那么多基于XML的Spring配置,所以理解如何在Spring中使用 ...