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]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
基本考虑到了,但是不严谨:首先要确定是坐标型n-1,还是序列型n吧
[英文数据结构或算法,为什么不用别的数据结构或算法]:
lambda:Arrays.sort(pairs, (a,b) -> (a[0] - b[0])); 名称,括号->括号
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- dp数组不能和题目给的数组搞混
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
基本考虑到了,但是不严谨:首先要确定是坐标型n-1,还是序列型n吧
[复杂度]:Time complexity: O(n^2) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
- class Solution {
- public int findLongestChain(int[][] pairs) {
- //corner case
- if (pairs == null || pairs.length == 0) return 0;
- //initialization: sort, fill the array with 1
- Arrays.sort(pairs, (a,b) -> (a[0] - b[0]));
- int[] dp = new int[pairs.length];
- Arrays.fill(dp, 1);
- //for loop for i and j
- for (int i = 0; i < pairs.length; i++) {
- for (int j = i + 1; j < pairs.length; j++) {
- dp[j] = Math.max(dp[j], pairs[j][0] > pairs[i][1] ? dp[i] + 1: dp[i]);
- }
- }
- return dp[pairs.length - 1];
- }
- }
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 ...
- 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 ...
- 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 链对的最大长度
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- [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 ...
- [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 ...
随机推荐
- Redis中存入存储的编码方式不一致解决问题
在利用redis缓存的时候,存入的数据与取出的数据编码方式不一致解决办法. from redis import StrictRedis #ecoding = 'utf-8',默认解码方式为bytes, ...
- 如果debug调试的时候中断总是停在析构函数的delete[] p上
如果debug调试的时候中断总是停在析构函数的delete[] p上,那可能 有两种情况: 1.调用析构函数的这个对象没有被分配空间,先找到调用调用析构函数出错的这个对象, 然后查看它是否被分配了空间 ...
- map/reduce/filter/lambda
Python内建了map()/reduce()/filter()函数. map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的It ...
- shell脚本大小写转换
几个方法 1.tr命令 2.sed替换 3.awk的tolower() toupper() 4.perl语言 详见 http://blog.51cto.com/wangxiaoyu/197623 L ...
- k8s Nodeport方式下service访问,iptables处理逻辑(转)
原文 https://www.myf5.net/post/2330.htm k8s Nodeport方式下service访问,iptables处理逻辑 2017年07月11日 0条评论 976次阅读 ...
- 02-Response简单响应报文
package com.day5; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputSt ...
- Memcached 使用备注
ICSharpCode.SharpZipLib未能加载文件或程序集 Memcached使用的是0.84版本的dll,vs2013带的是0.86版本 在web.config中<runtime> ...
- WebHttpRequest在sharepoint文档库中的使用
写在前面 由于sharepoint服务器上的站点采用的域用户windows认证的方式登陆,而app项目虽然能够提供用户名和密码,但客户是不愿意在网络上这样传输的.所以给提供了使用ssl证书认证的方式. ...
- 边缘触发(Edge Trigger)和条件触发(Level Trigger)
int select(int n, fd_set *rd_fds, fd_set *wr_fds, fd_set *ex_fds, struct timeval *timeout); sele ...
- mysql B+tree
什么是索引? 索引是为了加速对表中数据行的检索而创建的一种分散存储的数据结构. id和磁盘地址的映射. 关系型数据库存在磁盘当中. 为什要用索引? 索引能极大减少存储引擎需要扫描的数据量. 索引可以 ...