俄罗斯套娃
题目
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Note:
Rotation is not allowed.
Example:
Input: [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
解析重点
1.依题我们可以把寻找过程分为两步,第一步先对长进行排序,这样可以进行初步的嵌套,注意,这里对于相同的长度要按宽度反序排列,以便下一步使用。
2.第二步寻找最深的套娃,也就是寻找最长的升子序列,具体依赖于arr自带的二分查找。
java代码
1 | class Solution { |