当前位置:网站首页>LeetCode每日一题(1717. Maximum Score From Removing Substrings)
LeetCode每日一题(1717. Maximum Score From Removing Substrings)
2022-07-30 18:43:00 【wangjun861205】
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
}
}
边栏推荐
- OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
- 3D机器视觉厂商的场景争夺战役
- LeetCode Exercise - Two Questions About Finding Sum of Array Elements
- ByteArrayInputStream class source code analysis
- 单例模式 (Singleton)
- Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
- 开心的聚餐
- 第4章 控制执行流程
- 基础架构之Redis
- 中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
猜你喜欢

LeetCode Exercise - Two Questions About Finding Sum of Array Elements

kotlin的by lazy

Node encapsulates a console progress bar plugin

6块钱1斤,日本公司为何来中国收烟头?

Fixed asset visualization intelligent management system

微信小程序云开发 | 城市信息管理

基于inquirer封装一个控制台文件选择器

AI Basics: Graphical Transformer

【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest

【每日一道LeetCode】——191. 位1的个数
随机推荐
The use of terminal split screen tool Terminalx
requet.getHeader(“token“) 为null
arcpy获取要素类(属性表)包含的数目
CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配
Web结题报告
Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
Pytorch基础--tensorboard使用(一)
CCNA-子网划分(VLSM)
scrapy基本使用
第4章 控制执行流程
Scrapy框架介绍
ESP8266-Arduino编程实例-BMP180气压温度传感器驱动
基于inquirer封装一个控制台文件选择器
while,do while,for循环语句
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
高精度加法
Two-point answer naked question (plus a little pigeonhole principle)
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
【HMS core】【FAQ】HMS Toolkit典型问题合集1