-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path354.cpp
More file actions
68 lines (64 loc) · 2.43 KB
/
354.cpp
File metadata and controls
68 lines (64 loc) · 2.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
__________________________________________________________________________________________________
sample 20 ms submission
class Solution {
private:
/*
* This problem can be reduced to LIS (Longest Increasing Sub-sequence) problem.
* See LeetCode 300 - Longest Increasing Subsequence.
*
* In LIS problem, if we treat each number's index as w, and each number's value as h,
* then:
* 1. Any sub-sequence { a, b, c, ...} will satisfy w[a] < w[b] < w[c] < ...
* 2. Any increasing sub-sequence { a, b, c, ...} will satisfy h[a] < h[b] < h[c] < ...
*/
static int lengthOfLIS(const vector<int>& nums) {
vector<int> dp;
for (int num : nums) {
const auto it = lower_bound(dp.begin(), dp.end(), num);
if (it == dp.end())
dp.push_back(num);
else
*it = num;
}
return dp.size();
}
public:
/* time: O(n*log(n)), space: O(n) */
static int maxEnvelopes(vector<pair<int, int>>& envelopes) {
sort(envelopes.begin(), envelopes.end(), [](const auto& a, const auto& b) {
return (a.first == b.first) ? (a.second > b.second) : (a.first < b.first);
});
const int n = envelopes.size();
vector<int> nums(n);
for (int i = 0; i < n; ++i)
nums[i] = envelopes[i].second;
return lengthOfLIS(nums);
}
};
static int x = []() { ios::sync_with_stdio(false); cin.tie(NULL); return 0; }();
__________________________________________________________________________________________________
sample 11292 kb submission
class Solution {
public:
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
sort(envelopes.begin(), envelopes.end(), [](pair<int,int>& x, pair<int,int>& y) {
if (x.first == y.first)
return x.second > y.second;
return x.first < y.first;
});
vector<int> dp;
for (auto& p: envelopes) {
auto it = lower_bound(dp.begin(), dp.end(), p.second);
if (it == dp.end()) dp.push_back(p.second);
else *it = p.second;
/*
printf("%d %d ", p.first, p.second);
printf("it:%p dp:", it);
for (auto i: dp)
printf("%d ", i);
printf("\n");*/
}
return dp.size();
}
};
__________________________________________________________________________________________________