-
Notifications
You must be signed in to change notification settings - Fork 0
/
russian_doll_envelopes.py
38 lines (31 loc) · 1.15 KB
/
russian_doll_envelopes.py
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
35
36
37
38
"""
Task:
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 is greater than the width and height of the other envelope.
Return the maximum number of envelopes can you Russian doll (i.e., put one
inside the other). Note: You cannot rotate an envelope.
>>> envelopes = [[5, 4], [6, 4], [6, 7], [2, 3]]
>>> max_envelopes(envelopes)
3
>>> envelopes = [[1, 1], [1, 1], [1, 1]]
>>> max_envelopes(envelopes)
1
>>> envelopes = [[4,5],[4,6],[6,7],[2,3],[1,1]]
>>> max_envelopes(envelopes)
4
"""
def max_envelopes(envelopes):
res = [1 for _ in envelopes]
sorted_by_width = sorted(envelopes, key=lambda x: x[0])
for i in range(1, len(envelopes)):
for j in range(0, i):
if sorted_by_width[i][0] > sorted_by_width[j][0] and \
sorted_by_width[i][1] > sorted_by_width[j][1] and \
res[i] <= res[j]:
res[i] = res[j] + 1
return max(res)
if __name__ == '__main__':
import doctest
doctest.testmod()