Re: [閒聊] 每日leetcode已回收
題目:
給你一個字串
你可以消除中間的ab得到x分
或是消除中間的ba得到y分
問你最多能得幾分
思路:
題目的重點就是
像是aba
如果x>y就要選擇ab的組合 得到x分
然後消除ab
不然就是ba
所以需要對兩種情況做stack
然後要stack兩次
寫成函式之後比較簡潔了
```cpp
class Solution {
public:
string test(string s, char l, char r)
{
int len = s.size();
vector<char> res;
for(int i = 0 ; i < len ; i ++)
{
res.push_back(s[i]);
while(res.size()>1 && res[res.size()-2] == l && res[res.size()-1] ==
r)
{
res.pop_back();
res.pop_back();
}
}
string res2(res.begin(),res.end());
return res2;
}
int maximumGain(string s, int x, int y)
{
int res = 0;
if(x > y)
{
string ab = test(s,'a','b');
res += (s.size()-ab.size())/2 * x;
string ba = test(ab,'b','a');
res += (ab.size()-ba.size())/2 * y;
}
else
{
string ba = test(s,'b','a');
res += (s.size()-ba.size())/2 * y;
string ab = test(ba,'a','b');
res += (ba.size()-ab.size())/2 * x;
}
return res;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.13.212 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1720759462.A.47A.html
→
07/12 12:45,
1年前
, 1F
07/12 12:45, 1F
→
07/12 12:45,
1年前
, 2F
07/12 12:45, 2F
→
07/12 12:47,
1年前
, 3F
07/12 12:47, 3F
推
07/12 12:52,
1年前
, 4F
07/12 12:52, 4F
推
07/12 12:55,
1年前
, 5F
07/12 12:55, 5F
→
07/12 13:04,
1年前
, 6F
07/12 13:04, 6F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 476 之 1548 篇):