Maximum Length of Pair Chain
You are givenn
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 ifb < 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:
Note:
The number of given pairs will be in the range [1, 1000].
分析
因为可以任意顺序,但是每个Pair first<second,先排序
dp表示到当前i 最长的chain, 2层循环,为了及时返回,第二层倒序。
按照end排序,然后一个个数顺移计算res,同时更新end
不按start,因为可能第一个数范围就涵盖了后面所有数,更新的end也大,没有任何start能超过它。导致结果是1,具体解释:
https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105607/4-Liner-Python-Greedy
Last updated