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 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:
- 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)的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- [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 ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
随机推荐
- JavaFX——简单的日记系统
前言 在学习Swing后,听老师说使用Java写界面还可以使用JavaFX.课后,便去了解.JavaFX是甲骨文公司07年推出的期望应用于桌面开发领域的技术.在了解了这个技术几天后,便使用它完成Jav ...
- Python3+Selenium2完整的自动化测试实现之旅(二):IE和Chrome浏览器驱动配置
上一篇写了自动化测试需要的最基础环境配置,地址:https://www.cnblogs.com/tdp0108/p/10412073.html 当前市面上很多公司开发的应用软件都是BS架构,即基于浏览 ...
- Java的几个基本类型之间的相互转换
前言: 转载申明: 作者:王蒙 链接:http://matt33.com/2015/10/27/TheTransformOfJava/ 之前在写java程序的时候,经常会遇到很多的需要需要转换基础数据 ...
- JavaScript_02_JavaScript对象
ECMAScript标准中基本对象:Array,Boolean,Date,Math,Number,String,RegExp,Global Boolean,Date,Math,Number,Strin ...
- AI书单
1.<TensorFlow实战> 黄文坚 2.<Machine Learning> [美]Tom Mitchell 3.<机器学习> 周志华
- 环境搭建 - Tomcat(Windows)
Tomcat环境搭建 本文以Windows7下搭建tomcat-8.5.15为示例 下载tomcat压缩包 网址:Tomcat 非C盘根目录新建文件夹:Tomcat D:\tomcat 将tomcat ...
- Makedown
目录 Makedown 介绍 Markdown的语法 Makedown 介绍 Makedown的创建者是John Gruber Q:什么是markdown呢? markdown和html类似是mark ...
- spring----bean的使用
这篇文章不介绍spring的相关概念,只记录一下springbean的创建方式和使用方式. 一.bean的创建和调用 1.创建演示需要用到的类:Student.Teacher.Person packa ...
- 从.Net到Java学习第八篇——SpringBoot实现session共享和国际化
从.Net到Java学习系列目录 SpringBoot Session共享 修改pom.xml添加依赖 <!--spring session--> <dependency> & ...
- docker 学习资料收集
Docker中文网 http://www.docker.org.cn/book/ docker镜像怎么迁移到其他的服务器 http://www.talkwithtrend.com/Question/1 ...