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

题目:

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

题解:

Sort pairs first based on first element.

Use dp array, dp[i] means up to i, the maximum length of chain. For all j from 0 to i-1, if pairs[j][1] < pairs[i][0], dp[i] = Math.max(dp[i], dp[j]+1).

Time Complexity: O(n^2). n = pairs.length.

Space: O(n).

AC Java:

 class Solution {
public int findLongestChain(int[][] pairs) {
if(pairs == null || pairs.length == 0){
return 0;
} Arrays.sort(pairs, (a, b)->a[0]==b[0] ? a[1]-b[1] : a[0]-b[0]); int n = pairs.length;
int [] dp = new int[n];
for(int i = 0; i<n; i++){
dp[i] = 1;
for(int j = 0; j<i; j++){
if(pairs[j][1]<pairs[i][0]){
dp[i] = Math.max(dp[i], dp[j]+1);
}
}
} return dp[n-1];
}
}

按照pair的second number 排序pairs. 再iterate pairs, 若当前pair的second number 小于下个pair的first number, 计数res++, 否则跳过下个pair.

Note: 用curEnd把当前的second number标记出来, 不要用pair[i][1], 否则 i 跳动时就不是当前pair的second number了.

Time Complexity: O(nlogn). n = pairs.length.

Space: O(1).

AC Java:

 class Solution {
public int findLongestChain(int[][] pairs) {
Arrays.sort(pairs, (a,b) -> a[1]-b[1]);
int res = 0;
int curEnd = Integer.MIN_VALUE;
for(int [] pair : pairs){
if(pair[0] > curEnd){
res++;
curEnd = pair[1];
}
} return res;
}
}

类似Longest Increasing Subsequence.

LeetCode Maximum Length of Pair Chain的更多相关文章

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

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

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

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

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

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

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

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

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

  9. [LeetCode] 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. iOS 关于自动更新的分阶段发布(灰度发布)的相关简介

    前言:  AppStore 发布应用方式除了自动和手动,如今添加了分阶段发布(灰度发布).目的很明确,降低新版本骤然上升的bug率,不能挽回,只能发布新版本的风险.也也是针对禁止使用热修复,推出的相对 ...

  2. asp.net,缓存Cache

    缓存Cache: >直接使用Cache["content"],缓存与Session不同,所有用户都可以共享.永不过期,由服务器自己维护,当内存不够时,会将老的缓存释放掉. & ...

  3. 用js来实现那些数据结构 第二章

    这一篇文章,我们一起来看看数组还有哪些用法,以及在实际工作中我们可以用这些方法来做些什么.由于其中有部分内容并不常用,所以我尽量缩小篇幅.在这篇文章内介绍完大部分的数组方法,加快我们实现其它数据结构的 ...

  4. Linux Shell基础 通配符

    通配符 在 Bash 中,如果需要模糊匹配文件名或目录名,就要用到通配符.下面为常用的通配符. 表 1 通配符 通配符 作 用 ? 匹配一个任意字符 * 匹配 0 个或任意多个任意字符,也就是可以匹配 ...

  5. 0802 DRF 视图

    昨日回顾: 1. Serializer(序列化) 1. ORM对应的query_set和ORM对象转换成JSON格式的数据 1. 在序列化类中定义自定义的字段:SerializerMethodFiel ...

  6. UVA11383 Golden Tiger Claw

    题目 UVA11383 Golden Tiger Claw 做法 \(KM\)好题啊,满足所有边\(l(x)+l(y)≥w(x,y)\)(个人理解,如不对请及时留言),这样能满足\(\sum\limi ...

  7. 基于SSM的单点登陆03

    TbUser.java和TbUserExample.java,TbUserMapper.java,TbUserMapper.xml由mybatis框架生成. generatorConfig.xml & ...

  8. INSPIRED启示录 读书笔记 - 第34章 恐惧、贪婪、欲望

    消费者购买产品大多源于情感需求 企业级消费者出于恐惧和贪婪购买产品:如果不买这款产品,竞争对手会超过我,黑客会攻破我的防火墙,客户将弃我而去:如果买了,会赚得更多,省得更多 大众消费者购买产品的原因更 ...

  9. Go 字符串相关-标准库

    标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...

  10. Complex social network Partition for Balanced Subnetworks---Hao Lan Zhang,Jiming Liu,Chunyu Feng,Chaoyi Pang,Tongliang Li,Jing He阅读

    摘要:Abstract—Complex social network analysis methods have been applied extensively in various domains ...