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].

Runtime: 48 ms, faster than 63.85% of C++ online submissions for Maximum Length of Pair Chain.

排序然后two pointer。争取一次AC。

class Solution {
public:
static bool cmp(const vector<int>& a, const vector<int>& b){
return a[] < b[];
}
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(),pairs.end(),cmp);
int ret = , i = ,j = ;
while(j < pairs.size()){
if(pairs[i][] < pairs[j][]){
ret++;
i = j;
j++;
}else j++;
}
return ret+;
}
};

LC 646. Maximum Length of Pair Chain的更多相关文章

  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. 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 ...

  3. 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 ...

  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 second ...

  5. 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  6. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  7. [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 ...

  8. [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 ...

  9. LC 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 ...

随机推荐

  1. odoo标识符

    class Book(models.Model): _name = "library.book" _description = "Book" _order = ...

  2. deepin禁用笔记本自带键盘

    参考命令: sudo apt install xinput xinput xinput list-props 'AT Translated Set 2 keyboard' xinput set-pro ...

  3. Linux基础命令01

    绝对路径:从“/”根目录下开始 常用的一些命令选项 ls ls :使用方式  ls {空格}选项 {空格}参数 (全写为list)(等同于dir) 列出当前目录下所有的文件,包括隐藏文件 ls -a ...

  4. goaccess安装和使用

    安装依赖 $ sudo apt-get install libncursesw5-dev $ wget https://github.com/maxmind/geoip-api-c/releases/ ...

  5. Kaggle_泰坦尼克乘客存活预测

    转载 逻辑回归应用之Kaggle泰坦尼克之灾 此转载只为保存!!! ————————————————版权声明:本文为CSDN博主「寒小阳」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...

  6. 3.Fech_feed

    import tensorflow as tf # Fetch:可以在session中同时计算多个tensor或执行多个操作 # 定义三个常量 input1 = tf.constant(3.0) in ...

  7. QTP(9)

    常用的Windows控件 WinEdit---Set "数据值" SetSecure "加密数值" WinButton---Click WinComboBox- ...

  8. 在RecyclerView中集成QQ汽泡二

    上次已经将GooView集成到RecyclerView当中了[http://www.cnblogs.com/webor2006/p/7787511.html],但是目前还有很多问题,下面先来运行看一下 ...

  9. git fetch和pull的区别

    Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge   1 2 3 Git fetch origin master ...

  10. IO—转换流和键盘录入

    简单来说,由于方法的局限性和功能的需要,特此产生了转换流. InputStreamReader是字节流转换字符流的桥梁,为了提高效率,可以在缓冲区中放入转化流的对象,,并且构造函数第二个参数可以传入一 ...