354. Russian Doll Envelopes
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]).// Dynamic Programming
int maxEnvelopes(vector<vector<int>>& envelopes) { // time: O(n^2); space: O(n)
if (envelopes.empty()) return 0;
sort(envelopes.begin(), envelopes.end());
int n = envelopes.size();
// int res = 0;
vector<int> len(n, 1);
for (int i = 0; i < n; ++i) {
for (int j = i - 1; j >= 0; --j) {
if (envelopes[i][0] > envelopes[j][0] && envelopes[i][1] > envelopes[j][1])
len[i] = max(len[i], len[j] + 1);
}
// res = max(res, len[i]);
}
return *max_element(len.begin(), len.end());
}Last updated