Skip to content

Commit 4ec491a

Browse files
committed
russian_doll_envelopes
1 parent 0ddd7f3 commit 4ec491a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
251251
#### [347. Top K Frequent Elements](https://github.com/hitzzc/go-leetcode/tree/master/top_k_frequent_elements)
252252
#### [349. Intersection of Two Arrays](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_arrays)
253253
#### [350. Intersection of Two Arrays II](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_arrays_II)
254+
#### [354. Russian Doll Envelopes](https://github.com/hitzzc/go-leetcode/tree/master/russian_doll_envelopes)
254255

255256

256257

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution {
2+
public:
3+
intmaxEnvelopes(vector<pair<int, int>>& envelopes) {
4+
vector<int> dp;
5+
sort(envelopes.begin(), envelopes.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
6+
if (a.first == b.first) return a.second > b.second;
7+
return a.first < b.first;
8+
});
9+
for (int i = 0; i < envelopes.size(); ++i) {
10+
int left = 0, right = dp.size();
11+
while (left < right) {
12+
int mid = left + (right-left)/2;
13+
if (dp[mid] < envelopes[i].second) {
14+
left = mid+1;
15+
}else {
16+
right = mid;
17+
}
18+
}
19+
if (right >= dp.size()) dp.push_back(envelopes[i].second);
20+
else dp[right] = envelopes[i].second;
21+
}
22+
return dp.size();
23+
}
24+
};

0 commit comments

Comments
 (0)
close