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. sql 存储过程笔记2

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sys_Page_v2]') and OBJECTPROPE ...

  2. 【4】Zookeeper数据模型

    一.Znode节点是什么 1.1.概念   Znode节点是Zookeeper中数据模型中最小的数据单元.Zookeeper的数据模型是一颗树,由"/"进行分割路径.每个znode ...

  3. centos 7 Apache-Tomcat-8.5.46 安装 Web 应用服务器

    tomcat 官网版本地址:https://tomcat.apache.org/whichversion.html Servlet规格 JSP规范 EL规格 WebSocket规范 JASPIC规格 ...

  4. sql语句 小记录

    select Name '姓名',Age '年龄',(select LessonName + ',' from Lesson where StudentId=s1.Id FOR XML PATH('' ...

  5. Linux使用wget仿站

    运行命令 $ wget -r -p -np -k www.avatrade.cn 参数说明 -r --recursive(递归) specify recursive download.(指定递归下载) ...

  6. 关于shell中函数调用的理解

    今天做一个试题就是调用函数的问题,题意如下: 执行shell脚本,打印一个如下的水果菜单: 1.apple 2.pear 3.banana 4.cherry 当用户输入对应的数字选择水果的时候,告诉他 ...

  7. oracle学习1 基于oracle数据库的PLSQL编程以及存储过程的创建和使用视频

    https://www.bilibili.com/video/av46777605 plsql中选择testWindow中可以进行测试 1.编写函数在plsql的testwindow中 begin d ...

  8. 一键登录怎么在iOS端实现?这篇文章教会你!

    在一键登录出现之前,市场上最常见的APP 注册登录方式主要有账号密码.短信验证及第三方登录.这几种方式看似常见且便捷,实则存在许多安全隐患,用户体验也相对较差.首先,短信验证码到达率低.用户操作繁琐且 ...

  9. BZOJ 世界树

    第一步首先建虚树 第二步两遍dfs,一次从叶子到根,一次从根到叶子,就可以得到虚树中每个节点在M个询问点中离他最近的是哪个(简称为控制点) 第三步考虑计算答案,对于整个树,我们把节点化为三个种类 1. ...

  10. netty-1.从一个最简单的例子开始

    (原) 第一篇,从一个最简单的例子开始 1.netty是干什么,怎么用,这里不作介绍,先从一个例子来了解它, netty 5.0以上的版本被废弃了,以下例子从4.1.10.Final版本开始. 2.一 ...