-
Notifications
You must be signed in to change notification settings - Fork 28
/
Increasing_Subsequences.cpp
79 lines (74 loc) · 2.68 KB
/
Increasing_Subsequences.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
class Solution {
void findAllSubsequences(int prev, int indx, vector<int>& arr, vector<int>& subsequence, vector<vector<int>>& result) {
if(subsequence.size() > 1) {
result.push_back(subsequence);
}
unordered_set<int> taken;
for(int i = indx; i < (int)arr.size(); i++) {
if(arr[i] < prev) continue;
if(taken.find(arr[i]) != taken.end()) continue;
taken.insert(arr[i]);
subsequence.push_back(arr[i]);
findAllSubsequences(arr[i], i + 1, arr, subsequence, result);
subsequence.pop_back();
}
}
public:
vector<vector<int>> findSubsequences(vector<int>& arr) {
vector<vector<int>> result;
int n = (int)arr.size();
vector<int> subsequence;
findAllSubsequences(INT_MIN, 0, arr, subsequence, result);
return result;
}
};
// find all 'longest' subsequences
class Solution {
void findAllLongestSubsequences(int k, vector<vector<int>>& prevIndx, vector<int>& subsequence, vector<int>& arr, vector<vector<int>>& result) {
if(prevIndx[k].empty()) {
reverse(subsequence.begin(), subsequence.end());
result.push_back(subsequence);
}
for(int i = 0; i < prevIndx[k].size(); i++) {
subsequence.push_back(arr[prevIndx[k][i]]);
findAllLongestSubsequences(prevIndx[k][i], prevIndx, subsequence, arr, result);
subsequence.pop_back();
}
}
public:
vector<vector<int>> findSubsequences(vector<int>& arr) {
vector<vector<int>> result;
int n = (int)arr.size();
vector<int> dp(n);
vector<vector<int>> prevIndx(n, vector<int>());
int maxLen = 0;
vector<int> lastIndx;
for(int i = 0; i < n; i++) {
dp[i] = 1;
for(int j = 0; j < i; j++) {
if(arr[j] < arr[i]) {
if(dp[j] + 1 >= dp[i]) {
if(dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
prevIndx[i].clear();
}
prevIndx[i].push_back(j);
}
}
}
if(dp[i] >= maxLen) {
if(maxLen < dp[i]) {
maxLen = dp[i];
lastIndx.clear();
}
lastIndx.push_back(i);
}
}
for(int i = 0; i < lastIndx.size(); i++) {
vector<int> subsequence;
subsequence.push_back(arr[lastIndx[i]]);
findAllLongestSubsequences(lastIndx[i], prevIndx, subsequence, arr, result);
}
return result;
}
};