Array
Binary Search
Dynamic Programming
Sorting
Description
You are given a 2D array of integers envelopes
where envelopes[i] = [wi , hi ]
represents the width and the height of an envelope.
One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.
Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other) .
Note: You cannot rotate an envelope.
Example 1:
Input: envelopes = [[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]).
Example 2:
Input: envelopes = [[1,1],[1,1],[1,1]]
Output: 1
Constraints:
1 <= envelopes.length <= 105
envelopes[i].length == 2
1 <= wi , hi <= 105
Solutions
Solution 1
Python3 Java C++ Go
class Solution :
def maxEnvelopes ( self , envelopes : List [ List [ int ]]) -> int :
envelopes . sort ( key = lambda x : ( x [ 0 ], - x [ 1 ]))
d = [ envelopes [ 0 ][ 1 ]]
for _ , h in envelopes [ 1 :]:
if h > d [ - 1 ]:
d . append ( h )
else :
idx = bisect_left ( d , h )
d [ idx ] = h
return len ( d )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 class Solution {
public int maxEnvelopes ( int [][] envelopes ) {
Arrays . sort ( envelopes , ( a , b ) -> { return a [ 0 ] == b [ 0 ] ? b [ 1 ] - a [ 1 ] : a [ 0 ] - b [ 0 ] ; });
int n = envelopes . length ;
int [] d = new int [ n + 1 ] ;
d [ 1 ] = envelopes [ 0 ][ 1 ] ;
int size = 1 ;
for ( int i = 1 ; i < n ; ++ i ) {
int x = envelopes [ i ][ 1 ] ;
if ( x > d [ size ] ) {
d [++ size ] = x ;
} else {
int left = 1 , right = size ;
while ( left < right ) {
int mid = ( left + right ) >> 1 ;
if ( d [ mid ] >= x ) {
right = mid ;
} else {
left = mid + 1 ;
}
}
int p = d [ left ] >= x ? left : 1 ;
d [ p ] = x ;
}
}
return size ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 class Solution {
public :
int maxEnvelopes ( vector < vector < int >>& envelopes ) {
sort ( envelopes . begin (), envelopes . end (), []( const auto & e1 , const auto & e2 ) {
return e1 [ 0 ] < e2 [ 0 ] || ( e1 [ 0 ] == e2 [ 0 ] && e1 [ 1 ] > e2 [ 1 ]);
});
int n = envelopes . size ();
vector < int > d { envelopes [ 0 ][ 1 ]};
for ( int i = 1 ; i < n ; ++ i ) {
int x = envelopes [ i ][ 1 ];
if ( x > d [ d . size () - 1 ])
d . push_back ( x );
else {
int idx = lower_bound ( d . begin (), d . end (), x ) - d . begin ();
if ( idx == d . size ()) idx = 0 ;
d [ idx ] = x ;
}
}
return d . size ();
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 func maxEnvelopes ( envelopes [][] int ) int {
sort . Slice ( envelopes , func ( i , j int ) bool {
if envelopes [ i ][ 0 ] != envelopes [ j ][ 0 ] {
return envelopes [ i ][ 0 ] < envelopes [ j ][ 0 ]
}
return envelopes [ j ][ 1 ] < envelopes [ i ][ 1 ]
})
n := len ( envelopes )
d := make ([] int , n + 1 )
d [ 1 ] = envelopes [ 0 ][ 1 ]
size := 1
for _ , e := range envelopes [ 1 :] {
x := e [ 1 ]
if x > d [ size ] {
size ++
d [ size ] = x
} else {
left , right := 1 , size
for left < right {
mid := ( left + right ) >> 1
if d [ mid ] >= x {
right = mid
} else {
left = mid + 1
}
}
if d [ left ] < x {
left = 1
}
d [ left ] = x
}
}
return size
}
GitHub