1653. Minimum Deletions to Make String Balanced

该博客讨论了一个关于数组处理的问题,即如何在遇到连续的'a'字符时,通过翻转操作使得整个数组变为'b'。算法涉及动态维护翻转次数和'b'的计数,当'a'出现次数超过'b'时,重置翻转计数,以达到最小化翻转次数的目标。解决方案提供了一个C++类`Solution`,包含一个`minimumDeletions`方法,用于计算所需的最小翻转次数。
摘要由CSDN通过智能技术生成

Same problem with monotone fliping coin.

the machanics is like this, if you encounter b in side some a, you need to flip it, so you store the times you encounter the b.

but if you find out what if the whole array is b, or a, or a or b is monopoly in this array.

we assume the now we need to make the whole array to be b. so, every time you meet a, you add the fliping times by 1.

And you also count the occurence of b, if the occurence of b smaller than the b, you reset the flip, because you know in this prefix a shows up more than b, turn b to a is cost minmum, so reset the flip.

the flip is just like a flag standing in the postion j that the last b show up and tells you that from this to the i (s[i] = b), there are fliping times of occurrence of a. 

as i sad the flip is like a flag only stands at the b's position, if after the last b, there are no more b, we would increment the flip as these all a is invalid.

class Solution {
public:
    int minimumDeletions(string s) {
        int bcount =0, flip = 0;
        for(int i = 0; i < s.length(); i++){
            if(s[i] == 'b'){
                bcount++;
            }else{
                flip++;
            }
            flip = min(flip, bcount);
        }
        return flip;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值