码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题(1717. Maximum Score From Removing Substrings)


    You are given a string s and two integers x and y. You can perform two types of operations any number of times.

    Remove substring “ab” and gain x points.
    For example, when removing “ab” from “cabxbae” it becomes “cxbae”.
    Remove substring “ba” and gain y points.
    For example, when removing “ba” from “cabxbae” it becomes “cabxe”.
    Return the maximum points you can gain after applying the above operations on s.

    Example 1:

    Input: s = “cdbcbbaaabab”, x = 4, y = 5
    Output: 19

    Explanation:

    • Remove the “ba” underlined in “cdbcbbaaabab”. Now, s = “cdbcbbaaab” and 5 points are added to the score.
    • Remove the “ab” underlined in “cdbcbbaaab”. Now, s = “cdbcbbaa” and 4 points are added to the score.
    • Remove the “ba” underlined in “cdbcbbaa”. Now, s = “cdbcba” and 5 points are added to the score.
    • Remove the “ba” underlined in “cdbcba”. Now, s = “cdbc” and 5 points are added to the score.
      Total score = 5 + 4 + 5 + 5 = 19.

    Example 2:

    Input: s = “aabbaaxybbaabb”, x = 5, y = 4
    Output: 20

    Constraints:

    • 1 <= s.length <= 105
    • 1 <= x, y <= 104
    • s consists of lowercase English letters.

    假如 x < y, 那我们就优先消耗’ba’, 如果 x > y 则我们优先消耗’ab’。 其实我们需要考虑的就以下四中情况, ‘abab’, ‘baba’, ‘bbaa’, ‘aabb’


    impl Solution {
        pub fn maximum_gain(s: String, x: i32, y: i32) -> i32 {
            let mut stack: Vec<char> = Vec::new();
            let mut ans = 0;
            for c in s.chars() {
                if stack.is_empty() {
                    stack.push(c);
                    continue;
                }
                if c == if x <= y { 'a' } else { 'b' } {
                    let last = stack.pop().unwrap();
                    if last == if x <= y { 'b' } else { 'a' } {
                        ans += if x <= y { y } else { x };
                        continue;
                    }
                    stack.push(last);
                    stack.push(c);
                    continue;
                }
                stack.push(c);
            }
            let mut stack2 = Vec::new();
            for c in stack {
                if stack2.is_empty() {
                    stack2.push(c);
                    continue;
                }
                let last = stack2.pop().unwrap();
                if c == 'a' && last == 'b' {
                    ans += y;
                    continue;
                }
                if c == 'b' && last == 'a' {
                    ans += x;
                    continue;
                }
                stack2.push(last);
                stack2.push(c);
            }
            ans
        }
    }
    
    • 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
  • 相关阅读:
    简单记录一下在java的Mybatis-plus中用一个SQL语句查询一个嵌套的实体类(实体类中有List,List中还有List)
    能否在 vpp 外部释放 vpp 创建的 vlib buffer?
    【ASM】字节码操作 转换已有的类 在方法进入和退出的时候 添加
    gitlab离线安装时缺少依赖库的解决思路
    2022年湖北省中级工程师职称评审需要注意哪些问题呢?甘建二
    使用网络数据采集的好处
    一文搞懂Maven配置,从此不再糊涂下载依赖(文末有成品)
    *团和*滴2022秋招笔试知识点总结(超详细)
    常用设计模式学习总结
    数据库设计阶段-架构真题(五十七)
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126074510
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号