Skip to content

Commit c97aad5

Browse files
committed
day 30
1 parent 49bb015 commit c97aad5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Russian Doll Envelopes
3+
======================
4+
5+
You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.
6+
7+
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.
8+
Return the maximum number of envelopes can you Russian doll (i.e., put one inside the other).
9+
10+
Note: You cannot rotate an envelope.
11+
12+
Example 1:
13+
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
14+
Output: 3
15+
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
16+
17+
Example 2:
18+
Input: envelopes = [[1,1],[1,1],[1,1]]
19+
Output: 1
20+
21+
Constraints:
22+
1 <= envelopes.length <= 5000
23+
envelopes[i].length == 2
24+
1 <= wi, hi <= 104
25+
*/
26+
27+
class Solution
28+
{
29+
bool static isSmall(vector<int> &a, vector<int> &b)
30+
{
31+
return a[0] < b[0] && a[1] < b[1];
32+
}
33+
34+
public:
35+
int maxEnvelopes(vector<vector<int>> &env)
36+
{
37+
int ans = 1;
38+
int n = env.size();
39+
vector<int> dp(n, 1);
40+
sort(env.begin(), env.end());
41+
42+
for (int i = 1; i < n; ++i)
43+
{
44+
int prev = 0;
45+
for (int j = 0; j < i; ++j)
46+
{
47+
if (isSmall(env[j], env[i]))
48+
prev = max(prev, dp[j]);
49+
}
50+
dp[i] = prev + 1;
51+
ans = max(ans, dp[i]);
52+
}
53+
return ans;
54+
}
55+
};

Leetcode Daily Challenge/March-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
| 27. | [Palindromic Substrings](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/591/week-4-march-22nd-march-28th/3686/) | [cpp](./27.%20Palindromic%20Substrings.cpp) |
3232
| 28. | [Reconstruct Original Digits from English](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/591/week-4-march-22nd-march-28th/3687/) | [cpp](./28.%20Reconstruct%20Original%20Digits%20from%20English.cpp) |
3333
| 29. | [Flip Binary Tree To Match Preorder Traversal](https://leetcode.com/explore/featured/card/march-leetcoding-challenge-2021/592/week-5-march-29th-march-31st/3689/) | [cpp](./29.%20Flip%20Binary%20Tree%20To%20Match%20Preorder%20Traversal.cpp) |
34+
| 30. | [Russian Doll Envelopes](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/592/week-5-march-29th-march-31st/3690/) | [cpp](./30.%20Russian%20Doll%20Envelopes.cpp) |

0 commit comments

Comments
 (0)