forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-unequal-adjacent-groups-subsequence-ii.cpp
122 lines (115 loc) · 3.67 KB
/
longest-unequal-adjacent-groups-subsequence-ii.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
// Time: O(n^2)
// Space: O(n)
// dp, backtracing
class Solution {
public:
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
const auto& check = [&](const auto& s1, const auto& s2) {
if (size(s1) != size(s2)) {
return false;
}
int cnt = 0;
for (int k = 0; k < size(s1); ++k) {
cnt += s1[k] != s2[k] ? 1 : 0;
if (cnt == 2) {
return false;
}
}
return cnt == 1;
};
vector<pair<int, int>> dp(n, pair(1, -1));
int i = -1;
for (int i = n - 1; i >= 0; --i) {
const auto& si = words[i];
for (int j = i + 1; j < n; ++j) {
if (groups[i] != groups[j] && check(words[j], si)) {
dp[i] = max(dp[i], pair(dp[j].first + 1, j));
}
}
}
vector<string> result;
for (int i = distance(cbegin(dp), max_element(cbegin(dp), cend(dp)));i != -1; i = dp[i].second) {
result.emplace_back(words[i]);
}
return result;
}
};
// Time: O(n^2)
// Space: O(n)
// dp, backtracing
class Solution2 {
public:
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
const auto& check = [&](const auto& s1, const auto& s2) {
if (size(s1) != size(s2)) {
return false;
}
int cnt = 0;
for (int k = 0; k < size(s1); ++k) {
cnt += s1[k] != s2[k] ? 1 : 0;
if (cnt == 2) {
return false;
}
}
return cnt == 1;
};
vector<pair<int, int>> dp(n, pair(1, -1));
for (int i = 0; i < n; ++i) {
const auto& si = words[i];
for (int j = 0; j < i; ++j) {
if (groups[i] != groups[j] && check(words[j], si)) {
dp[i] = max(dp[i], pair(dp[j].first + 1, j));
}
}
}
vector<string> result;
for (int i = distance(cbegin(dp), max_element(cbegin(dp), cend(dp)));i != -1; i = dp[i].second) {
result.emplace_back(words[i]);
}
reverse(begin(result), end(result));
return result;
}
};
// Time: O(n^2)
// Space: O(n^2)
// list dp
class Solution3 {
public:
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
const auto& check = [&](const auto& s1, const auto& s2) {
if (size(s1) != size(s2)) {
return false;
}
int cnt = 0;
for (int k = 0; k < size(s1); ++k) {
cnt += s1[k] != s2[k] ? 1 : 0;
if (cnt == 2) {
return false;
}
}
return cnt == 1;
};
vector<vector<int>> dp(n);
for (int i = 0; i < n; ++i) {
const auto& si = words[i];
int mx_j = -1;
for (int j = 0; j < i; ++j) {
if (groups[i] != groups[j] && check(words[j], si) && (mx_j == -1 || size(dp[mx_j]) < size(dp[j]))) {
mx_j = j;
}
}
if (mx_j != -1) {
dp[i] = dp[mx_j];
}
dp[i].emplace_back(i);
}
const auto& ans = *max_element(cbegin(dp), cend(dp), [](const auto& a, const auto& b) {
return size(a) < size(b);
});
vector<string> result;
for (const auto& i : ans) {
result.emplace_back(words[i]);
}
return result;
}
};