原题链接在这里: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. Python3 optparse模块

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  2. 安装MySQL的详细步骤

    安装步骤如下: 1.打开网址:http://www.mysql.com/ ↓ 2.选择“Download”->“Windows”(此安装步骤只是在Window10 中进行,如有需要,其他系统可参 ...

  3. 微服务架构~BFF和网关是如何演化出来的

    介绍 BFF(Backend for Frontend)和网关Gateway是微服务架构中的两个重要概念,这两个概念相对比较新,有些开发人员甚至是架构师都不甚理解. 本文用假想的公司案例+图示的方式, ...

  4. java 断言工具类

    1.断言工具类 package com.sze.redis.util; import java.util.Collection; import java.util.Map; import com.sz ...

  5. awk的getline命令

    原文出处:生活费的博客,http://www.cnblogs.com/276815076/archive/2011/12/05/2276625.html#undefined  两种情况:getline ...

  6. php数组函数-array_merge()

    array_merge()函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值.如果数组是数字 索引,则键名会以连续方式重新索引. 注:如果仅仅向array_merg ...

  7. Go 文件操作

    一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...

  8. Go make 和 new的区别

    在Go语言中: make 被用来分配引用类型的内存: map, slice, channel new 被用来分配除了引用类型的所有其他类型的内存: int, string, array等 本文主要给大 ...

  9. jQuery 获取jsp页面中用iframe引入的jsp页面中的值

    <iframe scrolling="no" src="<c:url value='/unitBaseperson/view.do?para=9&op ...

  10. 获取浏览器的相关信息(navigator)

    * 智能机浏览器版本信息: * */ var browser = { versions: function() { var u = navigator.userAgent + navigator.ap ...