forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimum-ascii-delete-sum-for-two-strings.cpp
55 lines (49 loc) · 1.64 KB
/
minimum-ascii-delete-sum-for-two-strings.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
// Time: O(m * n)
// Space: O(n)
// DP with rolling window.
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
vector<vector<int>> dp(2, vector<int>(s2.length() + 1));
for (int j = 0; j < s2.length(); ++j) {
dp[0][j + 1] = dp[0][j] + s2[j];
}
for (int i = 0; i < s1.length(); ++i) {
dp[(i + 1) % 2][0] = dp[i % 2][0] + s1[i];
for (int j = 0; j < s2.length(); ++j) {
if (s1[i] == s2[j]) {
dp[(i + 1) % 2][j + 1] = dp[i % 2][j];
} else {
dp[(i + 1) % 2][j + 1] = min(dp[i % 2][j + 1] + s1[i],
dp[(i + 1) % 2][j] + s2[j]);
}
}
}
return dp[s1.length() % 2][s2.length()];
}
};
// Time: O(m * n)
// Space: O(m * n)
class Solution2 {
public:
int minimumDeleteSum(string s1, string s2) {
vector<vector<int>> dp(s1.length() + 1, vector<int>(s2.length() + 1));
for (int i = 0; i < s1.length(); ++i) {
dp[i + 1][0] = dp[i][0] + s1[i];
}
for (int j = 0; j < s2.length(); ++j) {
dp[0][j + 1] = dp[0][j] + s2[j];
}
for (int i = 0; i < s1.length(); ++i) {
for (int j = 0; j < s2.length(); ++j) {
if (s1[i] == s2[j]) {
dp[i + 1][j + 1] = dp[i][j];
} else {
dp[i + 1][j + 1] = min(dp[i][j + 1] + s1[i],
dp[i + 1][j] + s2[j]);
}
}
}
return dp[s1.length()][s2.length()];
}
};