leetcode-354-Russian Doll Envelopes
leetcode 354
问题
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:
1 | Input: [[5,4],[6,4],[6,7],[2,3]] |
分析
这道题与最长上升子数组有点类似,我们可以先将所有信封按照第一维进行升序排列,然后在第一维相同的情况下按照第二位进行降序排列。再在第二位上执行查找最长上升子序列的算法。重点是需要思考为什么第二维要按照降序进行排列,这样就可以避免对
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/23/leetcode-354/
License: 知识共享署名-非商业性使用 4.0 国际许可协议