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 core mvc 区域路由设置(配置)

    写博客原因:添加了区域(用作后台)后,报错: An unhandled exception occurred while processing the request.AmbiguousActionE ...

  2. 从零开始学安全(三十六)●利用python 爆破form表单

    import sys import requests from requests.auth import HTTPBasicAuth def Brute_Force_Web(postData): re ...

  3. PHP数组array_multisort排序详解

    今天特意再看了下官网的介绍,对它的多个数组的排序还是每台理解,找了些资料深入理解了下,在此总结下. PHP中array_multisort函数对多个数组或多维数组进行排序,关联(string)键名保持 ...

  4. 分析解剖微服务系列(二)-SOA和微服务异同

    微服务架构模式成熟之前,软件领域讨论的比较多的是SOA的架构模式.SOA早在1996年就由Gartner提出,作为面向服务的架构模式,SOA的理念是对于复杂的企业IT系统,按照不同的.可重用的粒度划分 ...

  5. JavaScript是如何工作的: Web推送通知的机制

    摘要: 如何在Web端推送消息? 这是专门探索 JavaScript 及其所构建的组件的系列文章的第9篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript是如何工作的:引擎,运行时 ...

  6. Asp.Net MVC @Html.TextBox 只允许输入数字问题

    转到Html.TextBox()看可以看出 Html.TextBox()方法,创建文本框[<input type="text"/>],并且可以带上name,value和 ...

  7. vue 音乐App QQ音乐搜索列表最新接口跨域设置

    在 webpack.dev.config.js中 'use strict' const utils = require('./utils') const webpack = require('webp ...

  8. 0.react学习笔记-环境搭建与脚手架

    0.环境搭建 笔者使用的是deepin/mac两种系统,因为两个电脑经常切换用.环境搭建没什么区别. 0.1 node安装 按照node官网叙述安装 # Using Debian, as root c ...

  9. 亿级流量场景下,大型缓存架构设计实现【1】---redis篇

    *****************开篇介绍**************** -------------------------------------------------------------- ...

  10. IDEA修改Git账户和密码

    找到c盘中git目录的.gitconfig文件可以直接修改name和邮箱