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. tp5将查询数据返回为对象转为数组

    use think\Model; collection()->toArray(); $result = collection(model("Menu")->order( ...

  2. STM32/EMC/ZILIAO

    https://www.st.com/content/ccc/resource/technical/document/application_note/a2/9c/07/d9/2a/b2/47/dc/ ...

  3. Hive权限管理(十)

    Hive权限管理 1.hive授权模型介绍 (1)Storage Based Authorization in the Metastore Server 基于存储的授权 - 可以对Metastore中 ...

  4. Tarjan无向图的割点和桥(割边)全网详解&算法笔记&通俗易懂

    更好的阅读体验&惊喜&原文链接 感谢@yxc的腿部挂件 大佬,指出本文不够严谨的地方,万分感谢! Tarjan无向图的割点和桥(割边) 导言 在掌握这个算法前,咱们有几个先决条件. [ ...

  5. string::compare

    1. compare string (1) 4int compare (const string& str) const noexcept; substrings (2) int compar ...

  6. ES head

    第2种安装方式 第二种方式就是不通过Elasticsearch插件方式进行安装 1.下载elasticsearch-head的源码包 地址:https://github.com/mobz/elasti ...

  7. vue基本语法 JS补充

    目录 一.VUE框架入门 1. vue框架的优势 二.VUE框架的基本使用 1. vue的引用 2. vue的基本语法结构 2. 插值表达式 3. 文本指令 (1)v-text (2)v-html ( ...

  8. CentOS下Hadoop运行环境搭建

    1.安装ssh免密登录 命令:ssh-keygen overwrite(覆盖写入)输入y 一路回车 将生成的密钥发送到本机地址 ssh-copy-id localhost (若报错命令无法找到则需要安 ...

  9. VUE: 移动端长按弹出确认删除地址(2)

    之前有一篇文章也写了长按弹出确认框的功能,在android机上测试过完全没问题,到后面整体测试时发现IOS这个功能长按移除就消失了, 除非长按不松手,用另外一只手点击确定才能完成操作,所以这次做了修改 ...

  10. Jmeter接口测试之用例数据分离

    之前我们的用例数据都是配置在 Jmeter Http 请求中,每次需要增加,修改用例都需要打开 jmeter 重新编辑,当用例越来越多的时候,用例维护起来就越来越麻烦,有没有好的方法来解决这种情况呢? ...