-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumDeletions.cpp
More file actions
39 lines (37 loc) · 1.13 KB
/
minimumDeletions.cpp
File metadata and controls
39 lines (37 loc) · 1.13 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
class Solution {
public:
int minimumDeletions(string s) {
int n = s.size();
int dp[2]{};
s[0] == 'a' ? dp[1] = 1 : dp[0] = 1;
for (int i = 1; i < n; ++i) {
if (s[i] == 'a') {
dp[0] = dp[0];
dp[1] = std::min(dp[1], dp[0]) + 1;
} else {
dp[1] = std::min(dp[1], dp[0]);
dp[0] = dp[0] + 1;
}
}
return std::min(dp[0], dp[1]);
}
};
class Solution {
public:
int minimumDeletions(string s) {
int del = 0;
for (char c : s)
del += 'b' - c; // 统计 'a' 的个数
int ans = del;
for (char c : s) {
// 'a' -> -1 'b' -> 1
del += (c - 'a') * 2 - 1;
ans = min(ans, del);
}
return ans;
}
};
// 作者:灵茶山艾府
// 链接:https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/solutions/2149746/qian-hou-zhui-fen-jie-yi-zhang-tu-miao-d-dor2/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。