【LeetCode】646.Maximum Length of Pair Chain(Medium)解题报告
来源:互联网 发布:序列密码算法rc4 编辑:程序博客网 时间:2024/06/07 15:12
【LeetCode】646.Maximum Length of Pair Chain(Medium)解题报告
题目地址: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.
Note:The number of given pairs will be in the range [1, 1000].Example1:
Input: [[1,2], [2,3], [3,4]]Output: 2Explanation: The longest chain is [1,2] -> [3,4]
又是dp,继续加深理解。
分析:[1,2],[2,3],[3,4]dp[i] : max length of chaindp[i] = Math.max(dp[i],dp[j]+1) if(dp[j][1] < dp[i][0] ) 0<=j<i
Solution:
class Solution { public int findLongestChain(int[][] pairs) { Arrays.sort(pairs,(a,b) -> a[0] - b[0]); int[] dp = new int[pairs.length]; for(int i=0 ; i<pairs.length; 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[pairs.length-1]; }}
Date:2017年11月15日
阅读全文
0 0
- 【LeetCode】646.Maximum Length of Pair Chain(Medium)解题报告
- LeetCode 646. Maximum Length of Pair Chain [Medium]
- Leetcode 646. Maximum Length of Pair Chain 找最长链 解题报告
- [leetcode]646. Maximum Length of Pair Chain
- leetcode 646. Maximum Length of Pair Chain
- LeetCode 646. Maximum Length of Pair Chain
- [Leetcode] 646. Maximum Length of Pair Chain
- leetcode 646. Maximum Length of Pair Chain
- [leetcode] 646. Maximum Length of Pair Chain
- [LeetCode] 646. Maximum Length of Pair Chain
- [LeetCode] 646. Maximum Length of Pair Chain
- LeetCode 646. Maximum Length of Pair Chain
- LeetCode:Maximum Length of Pair Chain
- [LeetCode]646. Maximum Length of Pair Chain<DP>
- leetcode 646. Maximum Length of Pair Chain(dp)
- Leetcode 646. Maximum Length of Pair Chain(算法分析week14)
- leetcode 646. Maximum Length of Pair Chain 最长连续元素
- 646. Maximum Length of Pair Chain
- 手机端HTML如何去掉滚动条的解决方案
- 加载R文件里面的内容
- 轻松入门 | 用 WordPress 和主题模板做网站
- VS2012常用快捷键
- 冒泡排序
- 【LeetCode】646.Maximum Length of Pair Chain(Medium)解题报告
- 网易2017内推笔试1:星际穿越 [python]
- 路由切换
- Chrome 63 Beta新特性介绍
- CtsPermissionTestCases android.permission.cts.DebuggableTest#testNoDebuggablefail
- subversion reported an error:The working copy at is too old(format 29) to work
- SpringBoot(一) 使用Eclipse搭建SpringBoot开发环境
- 爸比,什么是机器学习,机器能是如何学习的呀?
- android API 参考大全