-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path354.cpp
More file actions
33 lines (32 loc) · 969 Bytes
/
354.cpp
File metadata and controls
33 lines (32 loc) · 969 Bytes
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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 354
// Title: Russian Doll Envelopes
// Link: https://leetcode.com/problems/russian-doll-envelopes/
// Idea: Use Longest Increasing Subsequence - I used the O(n^2) version but
// there's definitely faster ones (O(n log n))
// Difficulty: medium
// Tags: longest-increasing-subsequence, dynamic-programming
class Solution {
public:
// speedup :D
Solution() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(), envelopes.end());
int n = envelopes.size();
if (n == 0) return 0;
vector<int> dp(n, 0);
for (int i = 0; i < n; ++i) {
dp[i] = 1;
for (int j = 0; j < i; ++j) {
if (envelopes[j][0] < envelopes[i][0] &&
envelopes[j][1] < envelopes[i][1]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
return *max_element(dp.begin(), dp.end());
}
};