当前位置:网站首页>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
}
}
边栏推荐
- 软件测试13年从业经验的前辈,总结的5条测试就业建议....
- 【总结】1396- 60+个 VSCode 插件,打造好用的编辑器
- Pytorch基础--tensorboard使用(一)
- 深化校企合作 搭建技术技能人才成长“立交桥”
- 网络基础(三)01-网络的基础概念——URL地址组成之协议、主机地址、路径和参数&127.0.0.1本地回环地址& 查看网址IP地址并访问之ping空格+网址&netstat -anb查看本机占用端口
- 延时队列优化 (2)
- AWS console
- CCNA-NAT协议(理论与实验练习)
- Two-point answer naked question (plus a little pigeonhole principle)
- core sound driver详解
猜你喜欢
while,do while,for循环语句
Fixed asset visualization intelligent management system
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
微信小程序云开发 | 城市信息管理
Mysql执行原理剖析
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
nlohmann json 使用指南【visual studio 2022】
Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
时序数据库在船舶风险管理领域的应用
node封装一个控制台进度条插件
随机推荐
积性函数
常见链表题及其 Go 实现
AWS 控制台
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
Chapter 4 Controlling the Execution Flow
【Qt Designer工具的使用】
Basic use of scrapy
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
Fixed asset visualization intelligent management system
6块钱1斤,日本公司为何来中国收烟头?
你好,我的新名字叫“铜锁/Tongsuo”
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
不同的路径依赖
怎么样的框架对于开发者是友好的?
好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%
生物医学论文有何价值 论文中译英怎样翻译效果好
原生js系列
LeetCode 练习——关于查找数组元素之和的两道题
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...