• 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
  • 相关阅读:
    C++PrimerPlus 第七章 函数-C++的编程模块-7.3 函数和数组
    点云从入门到精通技术详解100篇-三维点云属性变换编码(下)
    【动态规划刷题 10】最大子数组和 III && 环形子数组的最大和
    在 Web 项目中应用 Apache Shiro
    自己动手从零写桌面操作系统GrapeOS系列教程——12.QEMU+GDB调试
    Apache Drill 2万字面试题及参考答案
    使用sonar对webgoat进行静态扫描
    动态盘转换为基本盘
    高效畅通的iOS平台S5配置指南
    Android Jetpack 全家桶系列(一)起始篇:Jetpack 的前世今生
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126074510