当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢
ESP8266-Arduino programming example-BMP180 air pressure temperature sensor driver
MySql中@符号的使用
432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型
OSPF详解(3)
Recommended Books | Recommend 3 database books with rave reviews
Node encapsulates a console progress bar plugin
6块钱1斤,日本公司为何来中国收烟头?
ESP8266-Arduino编程实例-HC-SR04超声波传感器驱动
猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
随机推荐
3D机器视觉厂商的场景争夺战役
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
One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...
kotlin by lazy
The sixteenth issue of eight-part article Balabala said (MQ)
【剑指 Offe】剑指 Offer 17. 打印从1到最大的n位数
设计消息队列存储消息数据的 MySQL 表格
Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
requet.getHeader(“token“) 为null
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
《自然语言处理实战入门》---- 文本样本扩展小技巧:使用回译技术进行样本增强
几个GTest、GMock的例子
AI Basics: Graphical Transformer
ESP8266-Arduino programming example-HC-SR04 ultrasonic sensor driver
Read the "Language Model" in one article
【PHPWord】PHPOffice 套件之PHPWord快速入门
6块钱1斤,日本公司为何来中国收烟头?
【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显