LeetCode Maximum Length of Pair Chain
原题链接在这里: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:
- 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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- [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 ...
- 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 s ...
- 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] Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
随机推荐
- java Excel导入导出工具类
本文章,导入导出依赖提前定义好的模板 package com.shareworx.yjwy.utils; import java.io.File; import java.io.FileInputSt ...
- pdo封装2
<?php //添加了一个 _createSql 方法,负责创建所有sql class Db{ static private $ins; private $pdo; private $table ...
- Linux Graphic DRI Wayland 显示子系统
转:https://blog.csdn.net/u013165704/article/details/80709547 1. 前言 上篇文章(Linux graphic subsytem(1)_概述) ...
- JavaScript笔记04——事件与回调
1.在浏览器中,大多数代码都是由事件驱动的(event-driven). 这和生物中的神经反射有点类似. 比如说,谷歌页面上的一个按钮, 当我们“按下”这个按钮的时候,将跳出如下界面. 那么你有没想过 ...
- php数组函数-array_map()
array_map()函数返回用户自定义函数作用后的数组.回调函数接受的参数 数目应该和传递给array_map()函数的数组数目一直. array_map(function,array1,array ...
- IDEA: 遇到问题Error during artifact deployment. See server log for details.详解
IDEA 的配置确实有些烦人,完整的配置我之前发过,现在有个著名的报错: Error during artifact deployment. See server log for details. 这 ...
- Eclipse常用快捷键(转帖)
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- MySQL使用FEDERATED engine建立代理表
CREATE TABLE `yndzm` ( `city` varchar(40) DEFAULT NULL COMMENT '市(州)', `county` varchar(60) DEFAULT ...
- 区间dp的感悟
学区间dp似乎也很久了...对区间dp的通用模型都了解了一些 但是做题还是很坑 上了一点难度的题基本想不出什么思路.. 目前的做题方式就是看题 想一会发现自己不会做 看题解 好巧妙啊 理解后写一发.. ...
- Java日期时间输出格式优化
使用printf格式化日期 printf 方法可以很轻松地格式化时间和日期.使用两个字母格式,它以 %t 开头并且以下面表格中的一个字母结尾. 转 换 符 说 明 示 例 c 包括全部 ...