[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 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].
这道题给了我们一些链对,规定了如果后面链对的首元素大于前链对的末元素,那么这两个链对就可以链起来,问我们最大能链多少个。那么我们想,由于规定了链对的首元素一定小于尾元素,我们需要比较的是某个链表的首元素和另一个链表的尾元素之间的关系,如果整个链对数组是无序的,那么就很麻烦,所以我们需要做的是首先对链对数组进行排序,按链对的尾元素进行排序,小的放前面。这样我们就可以利用Greedy算法进行求解了。我们可以用一个栈,先将第一个链对压入栈,然后对于后面遍历到的每一个链对,我们看其首元素是否大于栈顶链对的尾元素,如果大于的话,就将当前链对压入栈,这样最后我们返回栈中元素的个数即可,参见代码如下:
解法一:
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
stack<vector<int>> st;
sort(pairs.begin(), pairs.end(), [](vector<int>& a, vector<int>& b) {
return a[] < b[];
});
for (auto pair : pairs) {
if (st.empty()) st.push(pair);
else {
auto t = st.top();
if (pair[] > t[]) st.push(pair);
}
}
return st.size();
}
};
我们可以对上面解法的空间进行优化,并不需要用栈来记录最长链上的每一个链对。而是用一个变量end来记录当前比较到的尾元素的值,初始化为最小值,然后遍历的时候,如果当前链对的首元素大于end,那么结果res自增1,end更新为当前链对的尾元素,参见代码如下:
解法二:
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
int res = , end = INT_MIN;
sort(pairs.begin(), pairs.end(), [](vector<int>& a, vector<int>& b) {
return a[] < b[];
});
for (auto pair : pairs) {
if (pair[] > end) {
++res;
end = pair[];
}
}
return res;
}
};
这道题论坛上最火的解法是用DP来做的,但是博主认为Greedy能解的就没有必要利用到DP,而且还不省空间,有点杀鸡用牛刀的感觉。不过话说这道题链来链去,为啥会让博主想到啥啥蜈蚣啊,什么鬼。。。
类似题目:
Longest Increasing Subsequence
参考资料:
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Maximum Length of Pair Chain 链对的最大长度的更多相关文章
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- 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 ...
- 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 ...
- [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】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 second ...
- [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
随机推荐
- 替换Java字符串中的“& lt;”为“<”
发布webservice时 Java中的String类型会将 “<” 自动转换为 “<”,在建String转换为XML时就会出错,具体做法是: String strXml = “< ...
- DotNetCore跨平台~Json动态序列化属性
回到目录 Json动态序列化属性,主要为了解决一个大实体,在返回前端时根据需要去序列化,如果实体里的某个属性在任务情况下都不序列化,可以添加[JsonIgnore]特性,这种是全局的过滤,但是更多的情 ...
- Beta Scrum Day 5
听说
- io多路复用(三)
#!/usr/bin/env python # -*- coding:utf-8 -*- import socket sk1 = socket.socket() sk1.bind(('127.0.0. ...
- js日常积累
1.数组转字符串 str.join(',') 2.字符串转数组 arr.split(',') 3.数组排序 function sorb(a,b){return a-b;}; arr.sort(sorb ...
- 如何进行服务器Linux系统下的ext文件系统修复
一.故障描述 服务器是dell 730系列服务器,存储阵列是MD3200系列存储5T的Lun,操作系统是Linux centos 7,文件系统类型是EXT4,因意外断电,导致系统不能正常启动,修复之后 ...
- 从同步阻塞聊到Java三种IO方式
本文总结自 https://zhuanlan.zhihu.com/p/34408883, https://www.zhihu.com/question/19732473中愚抄的回答, http://b ...
- Python设计TFTP客户端
#coding=utf-8 from socket import * from threading import Thread import struct def recvData(fileName, ...
- JaveScript内置对象(JS知识点归纳八)
1)JS自身提供的方式 用于对数据进行简便的操作,根据方法可以操作的数据类型不同,形成了不同的对象--内置对象 2)数组 a)基本操作方法--对数组进行修改 从数组最后进行操作 1)数组.push ...
- Python内置函数(64)——classmethod
英文文档: classmethod(function) Return a class method for function. A class method receives the class as ...