leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1717.cpp (664B)
0 class Solution {
1 public:
2 int maximumGain(const string s, int x, int y) const {
3 int res = 0, a = 0, b = 0, mini = min(x, y);
5 for (const char c : s) {
6 if (c > 'b') {
7 res += min(a, b) * mini;
8 a = b = 0;
9 continue;
10 }
12 if (c == 'a') {
13 if (x < y && b > 0)
14 b--, res += y;
15 else
16 a++;
17 } else {
18 if (x > y && a > 0)
19 a--, res += x;
20 else
21 b++;
22 }
23 }
25 return res + min(a, b) * mini;
26 }
27 };