Re: [闲聊] 每日leetcode

楼主: sixB (6B)   2024-07-30 19:58:09
: https://leetcode.com/problems/minimum-deletions-to-make-string-balanced
: ※ 引述《enmeitiryous (enmeitiryous)》之铭言:
: : 1653 minimun deletion make string balance
想说先把ab切出来
看起来比较清楚
要断在哪就把后面的a跟前面的b砍了
不过边分边扣好像就好了
不用开n
有多少扣多少 不用的补回去
意思一样
省流省空间
懒ㄉ改
class Solution {
public:
int minimumDeletions(string s) {
vector<int> a, b;
bool sw = true;
int cnt = 0;
for(char c: s){
if(sw){
if(c == 'a') cnt++;
else {
a.push_back(cnt);
cnt = 1;
sw = !sw;
}
}
else{
if(c == 'b') cnt++;
else{
b.push_back(cnt);
cnt = 1;
sw = !sw;
}
}
}
if(sw) a.push_back(cnt);
else b.push_back(cnt);
if(a.size() > b.size()) b.push_back(0);
int n = a.size();
int del = accumulate(a.begin(), a.end(), 0) - a[0];
int res = del;
for(int i = 1; i < n; i++){
del = del - a[i] + b[i-1];
res = min(res, del);
}
return res;
}
};

Links booklink

Contact Us: admin [ a t ] ucptt.com