leetcode:5634. 删除子字符串的最大得分(贪心,栈)

14 篇文章 1 订阅
9 篇文章 0 订阅

题目:

在这里插入图片描述

分析:

都想到了贪心,先ab或ba,选择分数大的。
奈何只想到了replace,没有想到栈,太菜了。

代码:

class Solution {
public:
    int maximumGain(string s, int x, int y) {
        stack<int> st;
        for(int i=s.size()-1;i>=0;i--)
        {
            st.push(s[i]);
        }
        int ans=0;
        if(x>y)
        {
            //先 ab 
            stack<int> st1;
            while(1)
            {
                if(st.empty()) break;
                if(st1.empty())
                {
                    st1.push(st.top());
                    st.pop();
                    continue;
                }
                if(st1.top()=='a'&&st.top()=='b') {
                    ans+=x;
                    st1.pop();st.pop();
                    continue;
                }
                st1.push(st.top());
                    st.pop();
            }
            while(!st1.empty()) {
                st.push(st1.top());
                st1.pop();
            }
            //再 ba
            stack<int> st2;
            while(1)
            {
                if(st.empty()) break;
                if(st2.empty())
                {
                    st2.push(st.top());
                    st.pop();
                    continue;
                }
                if(st2.top()=='b'&&st.top()=='a') {
                    ans+=y;
                    st2.pop();st.pop();
                    continue;
                }
                st2.push(st.top());
                    st.pop();
            }
        }
        else{
            //先 ab 
            stack<int> st1;
            while(1)
            {
                if(st.empty()) break;
                if(st1.empty())
                {
                    st1.push(st.top());
                    st.pop();
                    continue;
                }
                if(st1.top()=='b'&&st.top()=='a') {
                    ans+=y;
                    st1.pop();st.pop();
                    continue;
                }
                st1.push(st.top());
                    st.pop();
            }
            while(!st1.empty()) {
                st.push(st1.top());
                st1.pop();
            }
            //再 ba
            stack<int> st2;
            while(1)
            {
                if(st.empty()) break;
                if(st2.empty())
                {
                    st2.push(st.top());
                    st.pop();
                    continue;
                }
                if(st2.top()=='a'&&st.top()=='b') {
                    ans+=x;
                    st2.pop();st.pop();
                    continue;
                }
                st2.push(st.top());
                    st.pop();
            }
        }
        return ans;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值