leetCode 354. Russian Doll Envelopes

时间:2023-03-09 18:13:08
leetCode 354. Russian Doll Envelopes

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)

Example:
Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

Subscribe to see which companies asked this question

题目分析:目标求最大的信封数目,要求宽和高都要小于某个信封才能放入这个信封。

先排序:先按照宽排序,然后按照长排序,从小到大

结合题目分析:比如 1,20 2,21 3,4 4,5 5,6 ==>([3,4] => [4,5] => [5,6]).  3个!!!

          1,20 2,21 3,4 4,7 8,5==>  2个!!!

宽和高的顺序不能变化。

用数组c记录第i个信封,最大的重叠数目。

maxLen(c[i])=maxLen(c[j+1]) 或者 1