-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlis_lcs.cpp
More file actions
288 lines (267 loc) · 9.54 KB
/
lis_lcs.cpp
File metadata and controls
288 lines (267 loc) · 9.54 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Longest Increasing Subsequence (LIS) & Longest Common Subsequence (LCS)
Mathematical Foundation:
LIS: dp[i] = max length ending at i, Binary search O(n log n)
LCS: dp[i][j] = LCS of s1[0..i-1] and s2[0..j-1]
Time: LIS O(n log n), LCS O(n*m)
*/
#include <bits/stdc++.h>
using namespace std;
// LIS - O(n log n) with Binary Search
// LeetCode: 300. Longest Increasing Subsequence
// https://leetcode.com/problems/longest-increasing-subsequence/
// Related:
// 354. Russian Doll Envelopes
// https://leetcode.com/problems/russian-doll-envelopes/
// 646. Maximum Length of Pair Chain
// https://leetcode.com/problems/maximum-length-of-pair-chain/
// 673. Number of Longest Increasing Subsequence
// https://leetcode.com/problems/number-of-longest-increasing-subsequence/
// 1105. Filling Bookcase Shelves
// https://leetcode.com/problems/filling-bookcase-shelves/
// 435. Non-overlapping Intervals
// https://leetcode.com/problems/non-overlapping-intervals/
int lengthOfLIS(vector<int>& nums) {
vector<int> tails;
for (int x : nums) {
auto it = lower_bound(tails.begin(), tails.end(), x);
if (it == tails.end()) tails.push_back(x);
else *it = x;
}
return tails.size();
}
// LIS - Return actual sequence
// LeetCode: 300. Longest Increasing Subsequence (variant)
// https://leetcode.com/problems/longest-increasing-subsequence/
// Related:
// 673. Number of Longest Increasing Subsequence
// https://leetcode.com/problems/number-of-longest-increasing-subsequence/
// 354. Russian Doll Envelopes
// https://leetcode.com/problems/russian-doll-envelopes/
// 1048. Longest String Chain
// https://leetcode.com/problems/longest-string-chain/
// 368. Largest Divisible Subset
// https://leetcode.com/problems/largest-divisible-subset/
vector<int> findLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1), parent(n, -1);
int maxLen = 1, maxIdx = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
parent[i] = j;
}
}
if (dp[i] > maxLen) {
maxLen = dp[i];
maxIdx = i;
}
}
vector<int> lis;
int curr = maxIdx;
while (curr != -1) {
lis.push_back(nums[curr]);
curr = parent[curr];
}
reverse(lis.begin(), lis.end());
return lis;
}
// Longest Decreasing Subsequence
// Related LeetCode Problems:
// 300. Longest Increasing Subsequence (reverse approach)
// https://leetcode.com/problems/longest-increasing-subsequence/
// 354. Russian Doll Envelopes
// https://leetcode.com/problems/russian-doll-envelopes/
// 646. Maximum Length of Pair Chain
// https://leetcode.com/problems/maximum-length-of-pair-chain/
// 1105. Filling Bookcase Shelves
// https://leetcode.com/problems/filling-bookcase-shelves/
int lengthOfLDS(vector<int>& nums) {
vector<int> tails;
for (int x : nums) {
auto it = lower_bound(tails.begin(), tails.end(), x, greater<int>());
if (it == tails.end()) tails.push_back(x);
else *it = x;
}
return tails.size();
}
// Russian Doll Envelopes (2D LIS)
// LeetCode: 354. Russian Doll Envelopes
// https://leetcode.com/problems/russian-doll-envelopes/
// Related:
// 300. Longest Increasing Subsequence
// https://leetcode.com/problems/longest-increasing-subsequence/
// 646. Maximum Length of Pair Chain
// https://leetcode.com/problems/maximum-length-of-pair-chain/
// 435. Non-overlapping Intervals
// https://leetcode.com/problems/non-overlapping-intervals/
// 1048. Longest String Chain
// https://leetcode.com/problems/longest-string-chain/
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(), envelopes.end(), [](auto& a, auto& b) {
return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0];
});
vector<int> heights;
for (auto& e : envelopes) heights.push_back(e[1]);
return lengthOfLIS(heights);
}
// LCS - Standard DP
// LeetCode: 1143. Longest Common Subsequence
// https://leetcode.com/problems/longest-common-subsequence/
// Related:
// 72. Edit Distance
// https://leetcode.com/problems/edit-distance/
// 1035. Uncrossed Lines
// https://leetcode.com/problems/uncrossed-lines/
// 718. Maximum Length of Repeated Subarray
// https://leetcode.com/problems/maximum-length-of-repeated-subarray/
// 1092. Shortest Common Supersequence
// https://leetcode.com/problems/shortest-common-supersequence/
// 583. Delete Operation for Two Strings
// https://leetcode.com/problems/delete-operation-for-two-strings/
int longestCommonSubsequence(string text1, string text2) {
int m = text1.size(), n = text2.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
// LCS - Space Optimized
// LeetCode: 1143. Longest Common Subsequence (optimized)
// https://leetcode.com/problems/longest-common-subsequence/
// Related:
// 72. Edit Distance
// https://leetcode.com/problems/edit-distance/
// 97. Interleaving String
// https://leetcode.com/problems/interleaving-string/
// 115. Distinct Subsequences
// https://leetcode.com/problems/distinct-subsequences/
// 712. Minimum ASCII Delete Sum for Two Strings
// https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
int longestCommonSubsequenceOptimized(string text1, string text2) {
int m = text1.size(), n = text2.size();
if (m < n) swap(text1, text2), swap(m, n);
vector<int> dp(n + 1, 0);
for (int i = 1; i <= m; i++) {
int prev = 0;
for (int j = 1; j <= n; j++) {
int temp = dp[j];
if (text1[i - 1] == text2[j - 1]) dp[j] = prev + 1;
else dp[j] = max(dp[j], dp[j - 1]);
prev = temp;
}
}
return dp[n];
}
// Print LCS (Backtracking)
// LeetCode: 1143. Longest Common Subsequence (variant)
// https://leetcode.com/problems/longest-common-subsequence/
// Related:
// 1092. Shortest Common Supersequence
// https://leetcode.com/problems/shortest-common-supersequence/
// 583. Delete Operation for Two Strings
// https://leetcode.com/problems/delete-operation-for-two-strings/
// 712. Minimum ASCII Delete Sum for Two Strings
// https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
// 1035. Uncrossed Lines
// https://leetcode.com/problems/uncrossed-lines/
string printLCS(string text1, string text2) {
int m = text1.size(), n = text2.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
string lcs;
int i = m, j = n;
while (i > 0 && j > 0) {
if (text1[i - 1] == text2[j - 1]) {
lcs = text1[i - 1] + lcs;
i--; j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
return lcs;
}
// Shortest Common Supersequence
// LeetCode: 1092. Shortest Common Supersequence
// https://leetcode.com/problems/shortest-common-supersequence/
// Related:
// 1143. Longest Common Subsequence
// https://leetcode.com/problems/longest-common-subsequence/
// 583. Delete Operation for Two Strings
// https://leetcode.com/problems/delete-operation-for-two-strings/
// 712. Minimum ASCII Delete Sum for Two Strings
// https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
// 72. Edit Distance
// https://leetcode.com/problems/edit-distance/
string shortestCommonSupersequence(string str1, string str2) {
string lcs = printLCS(str1, str2);
string result;
int i = 0, j = 0, k = 0;
while (k < lcs.size()) {
while (i < str1.size() && str1[i] != lcs[k]) {
result += str1[i++];
}
while (j < str2.size() && str2[j] != lcs[k]) {
result += str2[j++];
}
result += lcs[k++];
i++; j++;
}
result += str1.substr(i) + str2.substr(j);
return result;
}
// Number of LIS
// LeetCode: 673. Number of Longest Increasing Subsequence
// https://leetcode.com/problems/number-of-longest-increasing-subsequence/
// Related:
// 300. Longest Increasing Subsequence
// https://leetcode.com/problems/longest-increasing-subsequence/
// 354. Russian Doll Envelopes
// https://leetcode.com/problems/russian-doll-envelopes/
// 646. Maximum Length of Pair Chain
// https://leetcode.com/problems/maximum-length-of-pair-chain/
// 1048. Longest String Chain
// https://leetcode.com/problems/longest-string-chain/
// 368. Largest Divisible Subset
// https://leetcode.com/problems/largest-divisible-subset/
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> len(n, 1), cnt(n, 1);
int maxLen = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
if (len[j] + 1 > len[i]) {
len[i] = len[j] + 1;
cnt[i] = cnt[j];
} else if (len[j] + 1 == len[i]) {
cnt[i] += cnt[j];
}
}
}
maxLen = max(maxLen, len[i]);
}
int result = 0;
for (int i = 0; i < n; i++) {
if (len[i] == maxLen) result += cnt[i];
}
return result;
}