Skip to content

Commit 54e62f6

Browse files
Add new solution
1 parent 4780333 commit 54e62f6

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Intuition
2+
The provided solution efficiently maximizes points by iterating through the string s twice, using a greedy approach to prioritize and remove pairs ("ab" or "ba") based on their respective point values (x or y). It employs a stack to track characters, ensuring that pairs are identified and removed correctly. By iterating through s in linear time per pass and using straightforward string operations, the solution optimally accumulates points and returns the maximum possible score after processing all valid pairs. This approach balances simplicity with effectiveness, leveraging intuitive greedy selection to solve the problem within practical runtime constraints.
3+
4+
Approach
5+
Problem Understanding: Recognize that the goal is to maximize points by repeatedly removing specific pairs ("ab" or "ba") from the given string s, where each removal yields a certain number of points (x for "ab" and y for "ba").
6+
7+
Greedy Strategy: Prioritize the removal of pairs based on their respective point values (x and y). This involves iterating through s twice:
8+
9+
First, remove "ab" pairs to maximize points using a stack to keep track of characters.
10+
Second, remove "ba" pairs similarly.
11+
This ensures that the maximum points are gained in each pass.
12+
Implementation Steps:
13+
14+
Define a helper function (removePairs) using a stack to manage characters while iterating through s.
15+
Accumulate points (tempPoints) for each pair removal within removePairs.
16+
Update the main function (maximumGain) to call removePairs twice: once for "ab" pairs and once for "ba" pairs.
17+
Accumulate and return the total points (points) gained from both passes to achieve the maximum possible score.
18+
Efficiency Considerations: Ensure the solution operates in linear time complexity relative to the length of s per pass, leveraging efficient stack operations and string manipulations to optimize performance. This approach balances simplicity with effectiveness, providing a clear path to solving the problem within practical runtime constraints.
19+
20+
Complexity
21+
Time complexity:
22+
TC : O(N)
23+
24+
Space complexity:
25+
SC : O(N)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution
2+
{
3+
4+
public:
5+
int maximumGain(string s, int x, int y)
6+
{
7+
int points = 0;
8+
;
9+
10+
auto removePairs = [&](char first, char second, int point)
11+
{
12+
string newString;
13+
int tempPoints = 0;
14+
for (char ch : s)
15+
{
16+
if (!newString.empty() && newString.back() == first && ch == second)
17+
{
18+
tempPoints += point;
19+
newString.pop_back();
20+
}
21+
else
22+
{
23+
newString.push_back(ch);
24+
}
25+
}
26+
s = newString;
27+
return tempPoints;
28+
};
29+
if (x > y)
30+
{
31+
points += removePairs('a', 'b', x);
32+
points += removePairs('b', 'a', y);
33+
}
34+
else
35+
{
36+
points += removePairs('b', 'a', y);
37+
points += removePairs('a', 'b', x);
38+
}
39+
return points;
40+
}
41+
};

0 commit comments

Comments
 (0)