forked from ligua/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-increasing-subsequence.cpp
183 lines (163 loc) · 4.57 KB
/
longest-increasing-subsequence.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Time: O(nlogn)
// Space: O(n)
// Binary search solution with STL.
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> LIS;
for (const auto& num : nums) {
insert(&LIS, num);
}
return LIS.size();
}
private:
void insert(vector<int> *LIS, const int target) {
// Find the first index "left" which satisfies LIS[left] >= target
auto it = lower_bound(LIS->begin(), LIS->end(), target);
// If not found, append the target.
if (it == LIS->end()) {
LIS->emplace_back(target);
} else {
*it = target;
}
}
};
// Binary search solution.
class Solution2 {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> LIS;
for (const auto& num : nums) {
insert(&LIS, num);
}
return LIS.size();
}
private:
void insert(vector<int> *LIS, const int target) {
int left = 0, right = LIS->size() - 1;
auto comp = [](int x, int target) { return x >= target; };
// Find the first index "left" which satisfies LIS[left] >= target
while (left <= right) {
int mid = left + (right - left) / 2;
if (comp((*LIS)[mid], target)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
// If not found, append the target.
if (left == LIS->size()) {
LIS->emplace_back(target);
} else {
(*LIS)[left] = target;
}
}
};
// Time: O(nlogn)
// Space: O(n)
// optimized from Solution4
class Solution3 {
public:
int lengthOfLIS(vector<int>& nums) {
set<int> sorted_nums(cbegin(nums), cend(nums));
unordered_map<int, int> lookup;
for (const auto& num : sorted_nums) {
lookup[num] = size(lookup);
}
SegmentTree segment_tree(size(lookup));
for (const auto& num : nums) {
segment_tree.update(lookup[num], lookup[num],
(lookup[num] >= 1) ? segment_tree.query(0, lookup[num] - 1) + 1 : 1);
}
return (size(lookup) >= 1) ? segment_tree.query(0, size(lookup) - 1) : 0;
}
private:
class SegmentTree {
public:
SegmentTree(int N)
: N_(N),
tree_(2 * N),
lazy_(N)
{
H_ = 1;
while ((1 << H_) < N) {
++H_;
}
}
void update(int L, int R, int h) {
L += N_; R += N_;
int L0 = L, R0 = R;
while (L <= R) {
if ((L & 1) == 1) {
apply(L++, h);
}
if ((R & 1) == 0) {
apply(R--, h);
}
L >>= 1; R >>= 1;
}
pull(L0); pull(R0);
}
int query(int L, int R) {
L += N_; R += N_;
auto result = 0;
push(L); push(R);
while (L <= R) {
if ((L & 1) == 1) {
result = max(result, tree_[L++]);
}
if ((R & 1) == 0) {
result = max(result, tree_[R--]);
}
L >>= 1; R >>= 1;
}
return result;
}
private:
int N_, H_;
vector<int> tree_, lazy_;
void apply(int x, int val) {
tree_[x] = max(tree_[x], val);
if (x < N_) {
lazy_[x] = max(lazy_[x], val);
}
}
void pull(int x) {
while (x > 1) {
x >>= 1;
tree_[x] = max(tree_[x * 2], tree_[x * 2 + 1]);
tree_[x] = max(tree_[x], lazy_[x]);
}
}
void push(int x) {
for (int h = H_; h > 0; --h) {
int y = x >> h;
if (lazy_[y] > 0) {
apply(y * 2, lazy_[y]);
apply(y * 2 + 1, lazy_[y]);
lazy_[y] = 0;
}
}
}
};
};
// Time: O(n^2)
// Space: O(n)
// Traditional DP solution.
class Solution4 {
public:
int lengthOfLIS(vector<int>& nums) {
const int n = nums.size();
vector<int> dp(n, 1); // dp[i]: the length of LIS ends with nums[i]
int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
res = max(res, dp[i]);
}
return res;
}
};