[leetcode-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 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[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]的更多相关文章
- 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(medium)
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 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 ...
- 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 ...
- [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] 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 ...
随机推荐
- mysql数据去重复distinct、group by
使用distinct 和group by都可以实现数据去重. select distinct 字段 group by 一般放在where条件后
- toad安装错误—Failed to Download products and updates
近日,在公司云电脑上安装Toad for oracle,安装到中途总会出现如下错误,个人认为是Toad安装时需要下载/更新一些组件,公司网络对下载有所限制,导致报错,无法进行后续安装. 图1.Toad ...
- Linux修改时区以及同步时间
Centos7为例:修改时区 timedatectl list-timezones |grep Shanghai #查找中国时区的完整名称 Asia/Shanghai timedatectl set- ...
- Linux分享笔记:系统状态检测命令小结
作为一名合格的运维人员,要能很好地了解Linux服务器,要能熟练查看Linux系统的运行状态.以下是常用到的Linux系统状态检测命令. 1. ifconfig:用于获取网卡配置与网络状态等信息.通常 ...
- JavaScript文本框焦点事件
效果图如下: <!-- 当文本框获得焦点时候,如果文本框内容是 请输入搜索关键字 清空文本框,输入内容变黑色 --> <!-- 当文本框失去焦点时候,如果文本框无内容,则添加灰色的 ...
- 使用hibernate框架连接oracle数据库进行简单的增删改
初始化配置和session 关于配置文件这里就不在赘述了,假设配置文件配好后我们需要加载配置和sessionFactory,并获取session,因为每次进行增删改查时都需要session,所以封装成 ...
- H5混合开发进阶
混合开发: 原生app里面,IOS 安卓的原生app里面,嵌套h5界面. 通过原生app里的一个webView盒子进行交互.webView是原生app内置的一个XXX,里面可以放置h5界面.可以相互调 ...
- scala映射和元组
scala映射,是一对键值对,相当于java中的Map 对偶:由两个值构成的组,形式 : 值1->值2,值1和值2类型不一定要相同,可以理解为对偶就是一个key/value 映射就是对偶的集合 ...
- Hadoop参数调优
转自:http://blog.sina.com.cn/s/blog_6a67b5c50100vop9.html dfs.block.size 决定HDFS文件block数量的多少(文件个数),它会间接 ...
- Hive初识(二)
Hive分区 Hive组织表到分区.它是将一个表到基于分区列,如日期,城市和部门的值相关方式.使用分区,很容易对数据进行部分查询. 表或分区是细分成桶,以提供额外的结构,可以使用更高效的查询的数据.桶 ...