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:

  1. Input: [[1,2], [2,3], [3,4]]
  2. Output: 2
  3. 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。

  1. class Solution {
  2. public:
  3. static bool cmp(const vector<int>& a, const vector<int>& b){
  4. return a[] < b[];
  5. }
  6. int findLongestChain(vector<vector<int>>& pairs) {
  7. sort(pairs.begin(),pairs.end(),cmp);
  8. int ret = , i = ,j = ;
  9. while(j < pairs.size()){
  10. if(pairs[i][] < pairs[j][]){
  11. ret++;
  12. i = j;
  13. j++;
  14. }else j++;
  15. }
  16. return ret+;
  17. }
  18. };

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. Darknet的整体框架,安装,训练与测试

    目录 一.Darknet优势 二.Darknet的结构 三.Darknet安装 四.Darknet的训练 五.Darknet的检测 正文 一.Darknet优势 darknet是一个由纯C编写的深度学 ...

  2. (备忘)Java数据类型中String、Integer、int相互间的转换

    1.Integer转换成int的方法 Integer i; int k = i.intValue();即Integer.intValue(); 2.int转换成Integer int i; Integ ...

  3. 【转】float与double的范围和精度

    原文:http://blog.csdn.net/wuna66320/article/details/1691734 1 范围 float和double的范围是由指数的位数来决定的. float的指数位 ...

  4. 在xshell中安装python3.6

    首先下载python安装包 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz 然后解压 tar Jxvf Python- ...

  5. 生产问题之泛型自动推断(JDK1.7新特性)

    今天提完代码,新来同事拉下代码后,如下代码出现异常: List<TblBlockMoneyDtl> transData = new ArrayList<>(); 分析原因后发现 ...

  6. five rendering ideas 里获取csm的 shadow边界做 pcf

    http://advances.realtimerendering.com/s2011/White,%20BarreBrisebois-%20Rendering%20in%20BF3%20(Siggr ...

  7. maven 项目下 Maven Dependencies 下列表为空

    问题如题,如下图: 解决: 选中 Maven Dependencies ,右键 属性 如下图: 把   resolve dependencies from workspace projects   这 ...

  8. 启动SpringBoot web项目出现 Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3,....

    详细错误信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> ...

  9. Acwing-168-生日蛋糕(搜索, 剪枝)

    链接: https://www.acwing.com/problem/content/170/ 题意: 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆 ...

  10. 题解 【POI2008】KUP-Plot purchase

    题面 先把题目意思讲一下吧: 给一个 \(n*n\) 的地图,每个格子有一个价格,找一个矩形区域,使其价格总和位于\([k,2k]\). 那么首先,可以想到,如果\(a[i][j]\)(格子的价格,下 ...