Russian Doll Envelopes

Question (LC.354)

Given a list of envelopes, find the maximum number of envelopes that one can Russian Doll.

An envelope is defined as [wi, hi] where w is the width and h is the height.

Envelope e1 can Russian Doll e2 if w1 > w2 and h1 > h2.

Examples

I: envelopes = [[5,4],[6,4],[6,7],[2,3]]
O: 3 ([2,3] -> [5,4] -> [6,7])

I: envelopes = [[1,1],[1,1],[1,1]]
O: 1

DP Approach

This is an optimization problem. We can frame it as a search problem and do DP.

An intuitive thing to do first is to sort by area. Because an envelop with a smaller area cannot Russian Roll with a bigger area.

Doll DAG

A dependency graph can be formed with a conditional to check if each edge is Russian Doll.

DP

Time O(n^2) Space O(n)

Greedy Approach

The DP solution can only pass 81 / 87 test cases. For input size over 5000, the run time between n^2 and nlogn will be quite significant (~1000x).

TBD

Last updated