当前位置:网站首页>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
}
}
边栏推荐
- ByteArrayInputStream 类源码分析
- 生物医学论文有何价值 论文中译英怎样翻译效果好
- 基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
- Anaconda Navigator stuck on loading applications
- The sixteenth issue of eight-part article Balabala said (MQ)
- 运营 23 年,昔日“国内第一大电商网站”黄了...
- MySQL数据类型
- core sound driver详解
- while,do while,for循环语句
- 沉浸式体验科大讯飞2022消博会“官方指定产品”
猜你喜欢

好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%

LeetCode 练习——关于查找数组元素之和的两道题

卫星电话是直接与卫星通信还是通过地面站?

Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法

What is the value of biomedical papers? How to translate the papers into Chinese and English?

Scrapy framework is introduced

Mysql execution principle analysis

MySQL数据类型

WeChat Mini Program Cloud Development | Urban Information Management
随机推荐
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
设计消息队列存储消息数据的 MySQL 表格
MySQL data types
Delay queue optimization (2)
Multiple instances of mysql
MySQL数据类型
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
《自然语言处理实战入门》---- 文本样本扩展小技巧:使用回译技术进行样本增强
二分答案裸题(加一点鸽巢原理)
CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri
常见链表题及其 Go 实现
积性函数
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
第14章 类型信息
3D机器视觉厂商的场景争夺战役
网络基础(二)-Web服务器-简介——WampServer集成服务器软件之Apache+MySQL软件安装流程 & netstat -an之检测计算机的端口是否占用
Mongo for infrastructure
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
The use of terminal split screen tool Terminalx