[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 ...
随机推荐
- Knowledge Point 20180305 机器数转换与进制转换
机器数(这里的机器数说的就是数值在计算机中的存储形式,相关可以了解数据在计算机中的表示)之间的转换往往是通过原码来实现的,下面我们结合进制来来一下: 进制也就是进位制,是人们规定的一种进位方法. 对于 ...
- RHEL6 建立DVD repo
1.准备镜像挂载目录 #mkdir /media/repo_dvd 2.制作镜像文件 #cp /dev/cdrom /opt/rhel_dvd.iso<BR> 或者 #dd if=/dev ...
- DBCacheServer升级
前段时间完成了该服务的设计的功能,花了很多时间和经历,最终完成了一个版本,已经测试了:现在后期再次在以前的基础上,完成了一些扩展. 1.扩展了内存存储 最初版本只是采用了gauva cache进行存储 ...
- vue入门:实现图片点击切换
1.实现功能 2.目录结构 3.代码 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- linux 执行程序时,提示not found问题分析
sh: ./test: not found 通常可以通过readelf查看该进程文件所以依赖的运行环境,检查相关路径是否存在对应的文件. 比如如下: 1. 检查/lib目录,发现ld-X.XX.so为 ...
- bzoj3895: 取石子(博弈论,记忆化搜索)
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 361 Solved: 177[Submit][Status][Discuss] D ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)-B-杨老师游戏
题目链接:杨老师游戏 题目分析:将9个数字分成3块,分块枚举,话句话说,9个数字的所有排列组合,如果满足N=a*b-c就是一个答案,暴力枚举Orz. 代码如下: #include<iostre ...
- Keepalived 配置高可用
VRRP协议及Keepalived原理使用 VRRP 协议即 Virtual Router Redundancy Protocol,虚拟路由器冗余协议, 为了解决局域网内默认网关单点失效的问题. ...
- nginx问题之nginx: could not build server_names_hash, you should increase server_names_hash_bucket_size解决方案
昨天在nginx上部署了一个网站后,发现访问不了,再去访问之前部署的网站,发现都访问不了了,去看下下nginx,发现nginx服务停止了,没有在运行,重启了下服务,发现还是一样,就去看了下nginx的 ...
- webpack 之 webpack-dev-server自动刷新
watch 首先介绍watch选项,参考这里.可实现相关源文件改变后自动更新bundle.js文件的功能.在配置文件中添加 watch:true 或执行 webpack -w,即可开启watch功能: ...