## 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)

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]).
```

## Rust Solution

``````struct Solution;

use std::cmp::Reverse;

impl Solution {
fn max_envelopes(mut envelopes: Vec<Vec<i32>>) -> i32 {
let n = envelopes.len();
envelopes.sort_unstable_by_key(|v| (v, Reverse(v)));
let mut dp = vec![];
for i in 0..n {
let height = envelopes[i];
if let Err(p) = dp.binary_search(&height) {
if p == dp.len() {
dp.push(height);
} else {
dp[p] = height;
}
}
}
dp.len() as i32
}
}

#[test]
fn test() {
let envelopes = vec_vec_i32![[5, 4], [6, 4], [6, 7], [2, 3]];
let res = 3;
assert_eq!(Solution::max_envelopes(envelopes), res);
let envelopes = vec_vec_i32![[4, 5], [4, 6], [6, 7], [2, 3], [1, 1]];
let res = 4;
assert_eq!(Solution::max_envelopes(envelopes), res);
}
``````

Having problems with this solution? Click here to submit an issue on github.