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[i] = max(dp[i],dp[j]+1);其中0=<j<i;

int findLongestChain(vector<vector<int>>& pairs)
{
sort(pairs.begin(), pairs.end(), [](const vector<int>&a, const vector<int>&b){return a[] < b[]; }); vector<int> dp(pairs.size(), );
for (int i = ; i < pairs.size(); i++)
{
for (int j = ; j<i;j++)
{
if (pairs[i][]>pairs[j][])
{
dp[i] = max(dp[i],dp[j]+);
}
} }
return *max_element(dp.begin(), dp.end());
}

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

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

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

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

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

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

  10. [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

随机推荐

  1. py faster rcnn的lib编译出错问题

    真是好事多磨啊,计算机系统依然是14.04,而cuda依然是8.0,唯一不同的是时间不一样,下载的各种库版本有差别,GPU的driver不一样. 但是这样就出问题了,py-faster rcnn的li ...

  2. android 学习笔记 杂记1

    getIntent().getExtras().get("intent"); 这个intent是数据包装的参数. 比如: Intent intent = new Intent(th ...

  3. Python基础—07-函数使用(01)

    #函数使用 零碎知识 灵活的if-else a = 3 if False else 5 # 等价于 if False: a = 3 else: a = 5 灵活的and/or # 当前面为真,才会进行 ...

  4. Excel导入功能(Ajaxfileupload)

    前言: 前端采用Easyui+Ajaxfileupload实现 后端采用springmvc框架,其中把解析xml封装成了一个jar包,直接调用即可 准备: 前端需要导入(easyui导入js省略,自行 ...

  5. HTML5读取input[type=file]中的图片

    转载 https://blog.csdn.net/fd214333890/article/details/71250488

  6. Java四舍五入时保留指定小数位数

    方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP).doubleValue(); ...

  7. 尚硅谷SpringBoot项目学习源码记录

    链接:https://pan.baidu.com/s/1aNUL1QlHMhDIFtWGhC1mtQ 密码:793v

  8. Ajax在表单中的应用

    ajax在注册用户表单中的使用 1.验证用户名是否被使用 2.获取手机短信验证码 3.点击表单中的图片刷新,可实现刷新图片验证码 <!DOCTYPE html> <html> ...

  9. 修改二维码生成插件jquery.qrcode.js支持加入自定义LOGO

    1,将jquery.qrcode.min.js和jquery添加到您的网页中 <script src="jquery.min.js"></script> & ...

  10. 从多Sheet的Excel文件中抽取数据

    在数据驱动管理中增加Excle的JDBCODBC驱动 类名:sun.jdbc.odbc.JdbcOdbcDriver URL模板:jdbc:odbc:driver={Microsoft Excel D ...