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 caseif (pairs == null || pairs.length == 0) return 0;//initialization: sort, fill the array with 1Arrays.sort(pairs, (a,b) -> (a[0] - b[0]));int[] dp = new int[pairs.length];Arrays.fill(dp, 1);//for loop for i and jfor (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];} }
View Code
转载于:https://www.cnblogs.com/immiao0319/p/9418696.html
646. Maximum Length of Pair Chain 最长的链条长度相关推荐
- leetcode 646. Maximum Length of Pair Chain | 646. 最长数对链(暴力递归->傻缓存->dp)
题目 https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题解 暴力递归->傻缓存->dp 写完 ...
- LeetCode 646. Maximum Length of Pair Chain
题目: You are given n pairs of numbers. In every pair, the first number is always smaller than the sec ...
- 最长数组对 Maximum Length of Pair Chain
为什么80%的码农都做不了架构师?>>> 问题: You are given n pairs of numbers. In every pair, the first numb ...
- 718. Maximum Length of Repeated Subarray 最长重复子数组
Title 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 **解释: ** ...
- 求两个数组的最长重复子数组 Maximum Length of Repeated Subarray
为什么80%的码农都做不了架构师?>>> 问题: Given two integer arrays A and B, return the maximum length of ...
- arry-718 Maximum Length of Repeated Subarray
题目:Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum le ...
- oracle sqlldr 数据导入错误Field in data file exceeds maximum length解决
使用oracle sqlldr进行数据导入时报Field in data file exceeds maximum length错误的解决办法: 一种是数据字段确实比数据库中的字段要长,这中错误需要调 ...
- oracle sqlldr 数据导入时报错:Field in data file exceeds maximum length完美解决
使用oracle sqlldr进行数据导入时报Field in data file exceeds maximum length错误的解决办法: 一种是数据字段确实比数据库中的字段要长,这中错误需要调 ...
- Maximum length exceeded错误的解决办法
一.问题 在序列化或反序列化对象时,由于对象数据量过大,超出了默认长度,引起程序抛出"maximum length exceeded"异常. 二.解决 在Web.config的&l ...
最新文章
- 酷狗音乐QQ显示(VC源代码)
- 基于投影仪的定位技术
- 操作系统:经典进程同步问题 之 生产者-消费者问题、读者-写者问题、哲学家进餐问题
- javaScript面向对象编程学习(二)
- 14年百度深度学习校招题目
- MFC处理回车窗口消失
- SVN登录时不断弹出用户名密码输入
- oracle edit历史,OGG-00952---oracle goldengate无法purge历史表和mark表处理一例
- asp.net mvc 2被遗忘的%:Html.AntiForgeryToken() %
- php后台+前端开发过程整理
- 银杏谷资本合伙人郑雨林:我为什么围绕阿里云生态做投资?
- webservice 实现通知支付结果到OA
- 保存的离线网页再打开会跳转的问题
- 数据库锁机制1------共享锁
- java实现给图片添加水印
- 蜡笔小新-java-map
- webpack 5高级配置优化
- ArcGIS平滑处理
- java多媒体教学软件-jteach
- 线性稳压器ME6209A33M3G应用电路
热门文章
- SQLITE3 使用总结(1)【ZT】
- shell编程追加1
- redis密码设置、访问权限控制等安全设置
- Web开发之二:什么是前端、什么是后端
- java 密码规则_密码规则(正则表达式)
- html改变占位字符的颜色,使用CSS更改HTML5输入的占位符颜色
- 【java】java wait 原理 synchronized ReentrantLock 唤醒顺序
- 【nginx】nginx 简介 基本概念 介绍
- [Flink] Flink运行报错Container released on a *lost* node
- Google Guice 一个轻量级的依赖注入框架