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 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].
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的更多相关文章
- 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 ...
- 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 ...
- 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】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- 第二章·Elasticsearch内部分片及分片处理机制介绍
一.副本分片介绍 什么是副本分片? 副本分片的主要目的就是为了故障转移,如果持有主分片的节点挂掉了,一个副本分片就会晋升为主分片的角色. 在索引写入时,副本分片做着与主分片相同的工作.新文档首先被索引 ...
- (十一)设置关闭多核cpu的核
echo 0 > /sys/devices/system/cpu/cpu3/online 查看当前有哪些核心 cat /sys/devices/system/cpu/online
- onItemSelected 获取选中的 信息 3种方法
@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) ...
- 【Swing】图形用户界面基础
前言 简单总结一下图形用户界面(Graphical User Interface)的相关基础,如GUI的基本元素:窗口,以及介绍Java中的图形界面开发设计的技术. 图形用户界面 图形用户界面就是以图 ...
- C++归并排序(数组&链表)
1.归并排序(Merge Sort) 归并排序的性能不受输入数据的影响,始终都是O(n log n)的时间复杂度.代价是需要额外的内存空间. 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采 ...
- 批量关闭linux进程
批量关闭linux进程 你是否经常遇到需要批量杀死很多进程的情况?而你是否还在一个一个的kill. 接下来我教你一个小秘诀吧. 1.首先我们查看当前的进程列表. 我们以查看nginx进程为例,通过ps ...
- vue 中echart折线自适应
前端时间做一个vue的项目,echart是按需引入的如下: // 引入 ECharts 主模块 import echarts from 'echarts/lib/echarts' // 引入折线图 i ...
- 回调函数c++类中实现
https://blog.csdn.net/mrailence/article/details/52251201 https://blog.csdn.net/qq_14820081/article/d ...
- 部署LVS-DR集群
设置Proxy代理服务器的VIP和DIP [root@proxy ~]# cd /etc/sysconfig/network-scripts [root@proxy network-scripts]# ...
- Autel MaxiIM IM608:如何更新和一些评论
MaxiIM IM608是最先进的,因此是与众不同的一种钥匙编程和诊断工具,它将先进的钥匙编程,所有系统医学和先进的服务融合在一个主要基于10.1英寸触摸屏的机械人中.它配备了XP400关键计算机用户 ...