id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int d=truckSize;\n vector<pair<int,int>>s;\n for(int i=0;i<boxTypes.size();i++)\n {\n s.push_back({boxTypes[i][1],boxTypes[i][0]});\n\n }\n\n sort(s.rbegin(),s.rend());\n for(auto y:s)\n {\n cout<<y.first<<y.second<<endl;\n }\n int ans=0;\n int first=0;int second=0;\n int tbox=0;\n bool flag=false;\n for(auto x:s)\n {\n second=x.second;\n first=x.first;\n tbox++;\n if(x.second>truckSize)\n {\n flag=true;\n break;\n }\n truckSize-=x.second;\n \n ans+=x.first*x.second;\n \n }\n if(flag==false)\n {\n return ans;\n }\n while(truckSize>0 )\n {\n if(second==0)\n {\n return ans;\n }\n ans+=first;\n truckSize--;\n second--;\n }\n\n return ans;\n }\n};",
"memory": "23700"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int t) {\n vector< pair<int,int> > v;\n for(int i = 0; i < boxTypes.size(); i ++){\n\n v.push_back({boxTypes[i][1], boxTypes[i][0]});\n\n }\n sort(v.rbegin(), v.rend());\n int ans = 0;\n int x = 0;\n for(int i = 0; i < v.size(); i ++){\n x = min(max(t,0), v[i].second);\n x *= v[i].first;\n ans += x;\n t -= v[i].second;\n\n }\n return ans;\n\n }\n};",
"memory": "23800"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<pair<int, int>> v;\n for (int i = 0; i < boxTypes.size(); i++) {\n v.push_back({boxTypes[i][1], boxTypes[i][0]});\n }\n sort(v.rbegin(), v.rend());\n int ans = 0;\n for (int i = 0; i < boxTypes.size(); i++) {\n if (v[i].second <= truckSize) {\n ans += v[i].first * v[i].second;\n truckSize -= v[i].second;\n }\n else {\n ans += v[i].first * truckSize;\n truckSize = 0;\n }\n }\n return ans;\n }\n};",
"memory": "23900"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int d=truckSize;\n vector<pair<int,int>>s;\n for(int i=0;i<boxTypes.size();i++)\n {\n s.push_back({boxTypes[i][1],boxTypes[i][0]});\n\n }\n\n sort(s.rbegin(),s.rend());\n for(auto y:s)\n {\n cout<<y.first<<y.second<<endl;\n }\n int ans=0;\n int first=0;int second=0;\n int tbox=0;\n bool flag=false;\n for(auto x:s)\n {\n second=x.second;\n first=x.first;\n tbox++;\n if(x.second>truckSize)\n {\n flag=true;\n break;\n }\n truckSize-=x.second;\n \n ans+=x.first*x.second;\n \n }\n if(flag==false)\n {\n return ans;\n }\n while(truckSize>0 )\n {\n if(second==0)\n {\n return ans;\n }\n ans+=first;\n truckSize--;\n second--;\n }\n\n return ans;\n }\n};",
"memory": "23900"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<vector<int>> sortedBoxes;\n int boxTypes_size = boxTypes.size();\n int truckTemp = truckSize;\n int maxUnits = 0;\n\n for (int i = 0; i < boxTypes_size; i++) {\n cout << \"boxTypes_size 1: \" << boxTypes_size << endl;\n cout << \"i 1: \" << i << endl;\n vector<int> biggestBox = boxTypes[i];\n int to_delete;\n for (int j = 0; j < boxTypes_size; j++) {\n if (boxTypes[j][1] >= biggestBox[1]) {\n biggestBox = boxTypes[j];\n to_delete = j;\n }\n }\n sortedBoxes.push_back(biggestBox);\n boxTypes.erase(boxTypes.begin() + to_delete);\n boxTypes_size--;\n i--;\n }\n\n for (int i = 0; i < sortedBoxes.size(); i++)\n cout << \"[\" << sortedBoxes[i][0] << \", \" << sortedBoxes[i][1] << \"]\" << endl;\n \n int i = 0;\n while (truckTemp > 0) {\n maxUnits += (sortedBoxes[i][0] * sortedBoxes[i][1]);\n truckTemp -= sortedBoxes[i][0];\n if (truckTemp <= 0) {\n for (int j = 0; j > truckTemp; j--) {\n maxUnits -= sortedBoxes[i][1];\n }\n break;\n }\n if (i == sortedBoxes.size() - 1) break;\n i++;\n }\n\n return maxUnits;\n }\n};",
"memory": "24300"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<pair<long long,long long>>vec;\n int n = boxTypes.size();\n for(int i=0 ; i<n ; i++){\n vec.push_back({boxTypes[i][1] , boxTypes[i][0]});\n }\n sort(vec.rbegin() , vec.rend());\n long long ans = 0;\n int left = truckSize;\n int i = 0;\n while(left > 0 && i<n){\n if(left >= vec[i].second){\n left -= vec[i].second;\n ans += vec[i].first * vec[i].second;\n }\n else{\n ans += vec[i].first * (long long)left;\n left = 0;\n }\n i++;\n }\n return ans;\n }\n};",
"memory": "24400"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector <vector<int>>boxTypesCopy;\n for (int i = 0;i<boxTypes.size();i++) {\n vector <int> temp = {boxTypes[i][1],boxTypes[i][0]};\n boxTypesCopy.push_back(temp);\n }\n sort(boxTypesCopy.begin(),boxTypesCopy.end(),std::greater<>());\n int res =0;\n int i = 0;\n while (truckSize > 0 && i <boxTypes.size()) {\n if (truckSize>=boxTypesCopy[i][1]) {\n res+=boxTypesCopy[i][1] * boxTypesCopy[i][0];\n truckSize-=boxTypesCopy[i][1];\n }\n else {\n res+=boxTypesCopy[i][0] * truckSize;\n break;\n }\n\n i++;\n }\n\n return res;\n }\n};",
"memory": "24500"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(std::vector<std::vector<int>>& boxTypes, int truckSize) {\n\n // create inventory <numberOfUnitsPerBox, numberOfBoxes>\n std::unordered_map<int, int> inventory;\n for (std::vector<int> boxType : boxTypes)\n inventory[boxType[1]] += boxType[0];\n\n // @note: boxTypes[i] = [numberOfBoxes_i, numberOfUnitsPerBox_i]\n // make two vectors: one for units per box, and other for the corresponding number of boxes\n std::set<int> unitsPerBox_set;\n for (std::vector<int> boxType : boxTypes) {\n unitsPerBox_set.insert(boxType[1]);\n }\n\n // convert the set back to vector\n std::vector<int> unitsPerBox;\n for (int boxType : unitsPerBox_set)\n unitsPerBox.push_back(boxType);\n // std::cout << \"vector: \";\n // for (int num : unitsPerBox) std::cout << num << \" \";\n // std::cout << std::endl;\n\n // sort unitsPerBox vector in descending order\n // quickSort(unitsPerBox, 0, unitsPerBox.size() - 1);\n\n // truck's loading process\n int totalUnitsLoaded = 0;\n int remainingSpace = truckSize;\n for (int i = unitsPerBox.size() - 1; i >= 0; i--) {\n if (remainingSpace <= 0) return totalUnitsLoaded;\n \n int unitsInThisBoxType = unitsPerBox[i];\n int availableBoxes = inventory[unitsInThisBoxType];\n\n // load the truck\n if (availableBoxes > remainingSpace) {\n totalUnitsLoaded += remainingSpace * unitsInThisBoxType;\n remainingSpace = 0;\n return totalUnitsLoaded;\n } else {\n totalUnitsLoaded += availableBoxes * unitsInThisBoxType;\n remainingSpace -= availableBoxes;\n }\n }\n\n return totalUnitsLoaded;\n }\n void quickSort(std::vector<int> &array, int startIdx, int endIdx) {\n if (startIdx >= endIdx) return;\n\n int pivotIdx = partition(array, startIdx, endIdx);\n quickSort(array, startIdx, pivotIdx - 1);\n quickSort(array, pivotIdx + 1, endIdx);\n }\nprivate:\n int partition(std::vector<int> &array, int startIdx, int endIdx) {\n int pivotValue = array[endIdx];\n int i = startIdx - 1;\n for (int j = startIdx; j <= endIdx - 1; j++) {\n if (array[j] > pivotValue) { // \"<\" for ascending order, \">\" for descending order\n i++;\n int temp = array[i];\n array[i] = array[j];\n array[j] = temp;\n }\n }\n i++;\n int temp = array[i];\n array[i] = array[endIdx];\n array[endIdx] = temp;\n return i;\n }\n};",
"memory": "26200"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& b, int t) \n {\n vector<vector<int>> v;\n for (int i = 0; i < b.size(); i++)\n {\n v.push_back({b[i][1], b[i][0]});\n }\n sort(v.rbegin(), v.rend());\n \n int ans = 0;\n for (int i = 0; i < v.size(); i++)\n {\n if (t <= 0)\n break;\n \n int boxesToTake = min(t, v[i][1]);\n ans += boxesToTake * v[i][0];\n t -= boxesToTake;\n }\n return ans;\n }\n};\n",
"memory": "26500"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n\n // maximum units : greedy put boxes with more units first\n // priority Queue: number of units : num of boxes\n priority_queue<vector<int>> my_heap; // max Heap\n\n int ans = 0; \n\n for (int i = 0; i < boxTypes.size(); i++)\n {\n vector<int> currVec; \n currVec.push_back(boxTypes[i][1]); // num units / box\n currVec.push_back(boxTypes[i][0]); // num of boxes\n\n // push on heap/priority queue with priority with units/box\n // max heap\n my_heap.push(currVec);\n }\n\n while ((!my_heap.empty()) && truckSize)\n {\n vector<int> currVec; \n // get box with maximum units in it\n currVec = my_heap.top();\n my_heap.pop();\n\n if (truckSize >= currVec[1])\n {\n // all boxes of this catagory can be fitted\n truckSize -= currVec[1];\n ans += (currVec[0] * currVec[1]);\n }\n else\n {\n // truck size is less. just put those many boxes and done\n ans += (truckSize * currVec[0]);\n\n truckSize -= truckSize; // effectively breaks the loop\n }\n }\n\n return ans; \n }\n};",
"memory": "27000"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n \n vector<vector<int>> temp;\n for(int i=0;i<boxTypes.size();i++)\n {\n vector<int> v;\n v.push_back(boxTypes[i][1]);\n v.push_back(boxTypes[i][0]);\n temp.push_back(v);\n }\n \n \n sort(temp.rbegin(),temp.rend());\n \n int box_cnt=0,units_cnt=0;\n for(int i=0;i<temp.size();i++)\n {\n if((box_cnt+temp[i][1])<truckSize)\n {\n box_cnt+=temp[i][1];\n units_cnt+=(temp[i][1]*temp[i][0]);\n }\n else \n {\n int qu=truckSize-box_cnt;\n box_cnt+=qu;\n units_cnt+=(qu*temp[i][0]);\n }\n\n }\n cout<<box_cnt;\n return units_cnt;\n }\n};",
"memory": "29200"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<vector<int>> sortedUnitsPerbox(boxTypes.size(),vector<int>(2));\n for(auto i : boxTypes){\n int numBoxes = i[0];\n int unitsPerBox = i[1];\n sortedUnitsPerbox.push_back({unitsPerBox,numBoxes});\n cout<<unitsPerBox<< \" \"<<numBoxes<<endl;\n }\n sort(sortedUnitsPerbox.rbegin(),sortedUnitsPerbox.rend());\n int maxprofit = 0;\n for(auto i : sortedUnitsPerbox){\n int unitsPerBox = i[0];\n int numBoxes = i[1];\n if(numBoxes <= truckSize){\n maxprofit += unitsPerBox*numBoxes;\n truckSize -= numBoxes;\n }\n else{\n maxprofit += truckSize*unitsPerBox;\n truckSize = 0;\n break;\n }\n }\n return maxprofit;\n\n }\n};",
"memory": "31800"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<vector<int>> sortedUnitsPerbox(boxTypes.size(),vector<int>(2));\n for(auto i : boxTypes){\n int numBoxes = i[0];\n int unitsPerBox = i[1];\n sortedUnitsPerbox.push_back({unitsPerBox,numBoxes});\n cout<<unitsPerBox<< \" \"<<numBoxes<<endl;\n }\n sort(sortedUnitsPerbox.rbegin(),sortedUnitsPerbox.rend());\n int maxprofit = 0;\n for(auto i : sortedUnitsPerbox){\n int unitsPerBox = i[0];\n int numBoxes = i[1];\n if(numBoxes <= truckSize){\n maxprofit += unitsPerBox*numBoxes;\n truckSize -= numBoxes;\n }\n else{\n maxprofit += truckSize*unitsPerBox;\n truckSize = 0;\n break;\n }\n }\n return maxprofit;\n\n }\n};",
"memory": "31800"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n static bool compareUnitSize(vector<int> & a, vector<int> b){\n return a[1] > b[1];\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(), compareUnitSize);\n\n int sumVal = 0;\n for(int i = 0; i < boxTypes.size(); i++){\n if(truckSize - boxTypes[i][0] >= 0){\n sumVal += (boxTypes[i][1] * boxTypes[i][0]);\n truckSize -= boxTypes[i][0];\n }\n else{\n sumVal += truckSize * boxTypes[i][1];\n break;\n }\n }\n return sumVal;\n }\n};",
"memory": "36400"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool sortcol(const vector<int>& v1,const vector<int> v2)\n {\n return v1[1]>v2[1];\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n \n sort(boxTypes.begin(),boxTypes.end(),sortcol);\n int ans=0;\n int t=0;\n while(truckSize>0 && t<boxTypes.size())\n {\n int boxCount=min(truckSize,boxTypes[t][0]);\n truckSize-=boxCount;\n ans+=(boxCount*boxTypes[t][1]);\n t++;\n }\n return ans;\n\n }\n};",
"memory": "36500"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool sortcol(const vector<int>& v1,const vector<int> v2)\n {\n return v1[1]>v2[1];\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n \n sort(boxTypes.begin(),boxTypes.end(),sortcol);\n int ans=0;\n int t=0;\n while(truckSize>0 && t<boxTypes.size())\n {\n int boxCount=min(truckSize,boxTypes[t][0]);\n truckSize-=boxCount;\n ans+=(boxCount*boxTypes[t][1]);\n t++;\n }\n return ans;\n\n }\n};",
"memory": "36500"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int boxes = 0, count = 0, sumBox = 0;;\n sort(boxTypes.begin(), boxTypes.end(), [](const vector<int>& a, const vector<int>b){\n return a[1] > b[1]; \n });\n int totalUnits = 0;\n for (const auto& box : boxTypes) {\n int numBoxes = box[0];\n int unitsPerBox = box[1];\n int boxesToLoad = min(numBoxes, truckSize);\n totalUnits += boxesToLoad * unitsPerBox;\n truckSize -= boxesToLoad;\n if (truckSize <= 0) {\n break;\n }\n }\n return totalUnits;\n }\n \n};",
"memory": "36600"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Compare {\npublic:\n bool operator()(vector<int> below, vector<int> above)\n {\n return below[1] < above[1] ;\n }\n};\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n priority_queue<vector<int>,vector<vector<int>>,Compare>pq;\n for(auto &v:boxTypes) pq.push(v);\n int ans =0;\n while(truckSize>0 && !pq.empty()){\n auto v = pq.top();\n pq.pop();\n if(truckSize <= v[0]){\n ans += truckSize * v[1];\n }else{\n ans+= v[1] * v[0];\n }\n truckSize -=v[0];\n }\n return ans;\n }\n};",
"memory": "49300"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool custom(vector<int>a ,vector<int>b){\n return b[1]<a[1];\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int units=0; \n sort(boxTypes.begin(),boxTypes.end(),custom);\n\n int i=0;\n while(truckSize>0 && i<boxTypes.size()){\n if(truckSize>boxTypes[i][0]){\n units+=boxTypes[i][0]*boxTypes[i][1];\n }\n else{\n units+=truckSize*boxTypes[i][1];\n }\n truckSize=truckSize-boxTypes[i][0];\n i++;\n }\n return units;\n \n }\n};\n",
"memory": "53900"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(), [](vector<int> a, vector<int> b) {\n return a[1] > b[1];\n });\n\n int left = truckSize;\n int units = 0;\n int boxes = 0;\n int cantake = 0;\n \n for (int i = 0; i < boxTypes.size(); i++) {\n if(left < 0) {\n break;\n }\n boxes += min(boxTypes[i][0], left);\n units += boxTypes[i][1] * min(boxTypes[i][0], left);\n left -= min(boxTypes[i][0], left);\n }\n \n\n // for (int i = 0; i < boxTypes.size(); i++) {\n // cout << boxTypes[i][0] << \" \" << boxTypes[i][1] << endl;\n // }\n\n return units;\n }\n};",
"memory": "54000"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool comp(vector<int> a, vector<int> b) { return a[1] > b[1]; }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(), comp);\n\n int totalUnits = 0;\n int index = 0;\n\n while (truckSize > 0 && index < boxTypes.size()) {\n int boxesToTake = min(boxTypes[index][0], truckSize);\n totalUnits += boxesToTake * boxTypes[index][1];\n truckSize -= boxesToTake;\n index++;\n }\n\n return totalUnits;\n }\n};\n",
"memory": "54100"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n // disable sync of C++ i/o stream with C i/o stream\n ios::sync_with_stdio(false);\n cin.tie(0); // break the sync of cin & cout flusing so when cin is used cout is not flushed\n // ignore the ifrst two lines they are just there to solve c++ innternal \n // issue of slow down becasue of syncing c-style scanf & printf to cin cout\n auto cmp = [](vector<int> a, vector<int> b){return a[1]>b[1];};\n int units = 0;\n std::sort(boxTypes.begin(), boxTypes.end(), cmp);\n for (auto &box : boxTypes){\n if (truckSize <= 0)\n break;\n if(truckSize >= box[0]){\n truckSize -= box[0];\n units += (box[1] * box[0]);\n } else {\n units += (box[1] * truckSize);\n truckSize = 0;\n }\n }\n\n return units;\n}\n};",
"memory": "54100"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // Comparator function for sorting the array w.r.t to no. of units\n static bool comparator(vector<int> a,vector<int> b)\n {\n return(a[1]>b[1]);\n }\n \n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n // Sort the array\n sort(boxTypes.begin(),boxTypes.end(),comparator);\n int curr = 0; // curr for storing the no. of boxes used\n int fina = 0; // fina for storing the no. of units taken\n \n \n for(int i =0;i<boxTypes.size();i++)\n {\n // adding boxes until it crosses the trucksize and adding no. of units also\n if((curr+boxTypes[i][0])<truckSize)\n {\n curr += boxTypes[i][0];\n fina += (boxTypes[i][0]*boxTypes[i][1]);\n }\n // when the truck is partially filled with boxes and we have to take some boxes from the no. of boxes so we calculate the remain boxes and with respect add the no. of units also\n else\n {\n int remain = truckSize - curr;\n fina += (remain*boxTypes[i][1]);\n break;\n }\n }\n return fina;\n }\n};",
"memory": "54200"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(), [&](auto b1, auto b2) {\n return b1[1] > b2[1];\n });\n int res = 0;\n for(auto &it : boxTypes) {\n int boxTaken = min(truckSize, it[0]);\n res += boxTaken * it[1];\n truckSize -= boxTaken;\n }\n return res;\n }\n};",
"memory": "54200"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool custom(vector<int>a ,vector<int>b){\n return b[1]<a[1];\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int units=0; \n sort(boxTypes.begin(),boxTypes.end(),custom);\n\n int i=0;\n while(truckSize>0 && i<boxTypes.size()){\n if(truckSize>boxTypes[i][0]){\n units+=boxTypes[i][0]*boxTypes[i][1];\n }\n else{\n units+=truckSize*boxTypes[i][1];\n }\n truckSize=truckSize-boxTypes[i][0];\n i++;\n }\n return units;\n \n }\n};\n",
"memory": "54300"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool custom(vector<int>a ,vector<int>b){\n return b[1]<a[1];\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int units=0; \n sort(boxTypes.begin(),boxTypes.end(),custom);\n\n int i=0;\n while(truckSize>0 && i<boxTypes.size()){\n int mini=min(truckSize,boxTypes[i][0]);\n units+=mini*boxTypes[i][1];\n truckSize=truckSize-mini;\n i++;\n }\n return units;\n \n }\n};",
"memory": "54300"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool comp(vector<int> a,vector<int> b){\n return a[1] > b[1];\n }\n\n int maximumUnits(vector<vector<int>>& arr, int truckSize){\n int store = 0;\n sort(arr.begin(),arr.end(),comp);\n for(int i=0;i<arr.size();i++){\n if(arr[i][0]<=truckSize){\n store += arr[i][0]*arr[i][1];\n truckSize = truckSize - arr[i][0];\n }\n else{\n store += truckSize * arr[i][1];\n break;\n }\n }\n return store;\n }\n};",
"memory": "54400"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool static customcomp(vector<int> i, vector<int> j){\n return i[1] == j[1] ? i[0] >= j[0] : i[1] > j[1];\n }\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(), customcomp);\n int count = 0, k = 0, ans = 0;\n\n while(count < truckSize && k < boxTypes.size()){\n int tempc = min(truckSize - count, boxTypes[k][0]);\n ans += tempc * boxTypes[k][1];\n count += tempc;\n k++;\n }\n\n\n return ans;\n }\n};",
"memory": "54500"
} |
1,829 | <p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p>
<ul>
<li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li>
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li>
</ul>
<p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed <code>truckSize</code>.</p>
<p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
<strong>Output:</strong> 91
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= boxTypes.length <= 1000</code></li>
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(), [](vector<int> a, vector<int> b){\n return a[1]>b[1];\n });\n int ans=0;\n for(auto x:boxTypes){\n if(x[0]<=truckSize){\n ans+=x[1]*x[0];\n truckSize-=x[0];\n } else {\n ans+=truckSize*x[1];\n break;\n }\n }\n return ans;\n }\n};",
"memory": "55000"
} |
1,817 | <p>Hercy wants to save money for his first car. He puts money in the Leetcode bank <strong>every day</strong>.</p>
<p>He starts by putting in <code>$1</code> on Monday, the first day. Every day from Tuesday to Sunday, he will put in <code>$1</code> more than the day before. On every subsequent Monday, he will put in <code>$1</code> more than the <strong>previous Monday</strong>.<span style="display: none;"> </span></p>
<p>Given <code>n</code>, return <em>the total amount of money he will have in the Leetcode bank at the end of the </em><code>n<sup>th</sup></code><em> day.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 10
<strong>Explanation:</strong> After the 4<sup>th</sup> day, the total is 1 + 2 + 3 + 4 = 10.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 37
<strong>Explanation:</strong> After the 10<sup>th</sup> day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2<sup>nd</sup> Monday, Hercy only puts in $2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 20
<strong>Output:</strong> 96
<strong>Explanation:</strong> After the 20<sup>th</sup> day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int totalMoney(int n) {\n if(n <= 7){\n return n * (n+1) / 2;\n }\n return 28 + (n-7) + totalMoney(n-7);\n }\n};",
"memory": "7100"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string& s, int x, int y) {\n if (x < y){\n swap(x, y);\n reverse(s.begin(), s.end());\n }\n int aCount = 0;\n int bCount = 0;\n int totalPoints = 0;\n\n for (int i = 0; i < s.size(); ++i){\n char currentChar = s[i];\n if (currentChar == 'a') ++aCount;\n else if (currentChar == 'b'){\n if (aCount > 0){\n --aCount;\n totalPoints += x;\n }\n else {\n ++bCount;\n }\n }\n else {\n totalPoints += min(bCount, aCount) * y;\n aCount = bCount = 0;\n }\n }\n\n totalPoints += min(bCount, aCount) * y;\n return totalPoints;\n }\n};",
"memory": "15909"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumGain(string& s, int x, int y) {\n string priority = (x > y) ? \"ab\" : \"ba\";\n int A_count = 0;\n int B_count = 0;\n\n int result = 0;\n for (char c : s) {\n if (c == 'a') {\n A_count++;\n\n if (priority == \"ba\" && B_count > 0) {\n result += y;\n\n A_count--, B_count--;\n }\n } else if (c == 'b') {\n B_count++;\n\n if (priority == \"ab\" && A_count > 0) {\n result += x;\n\n A_count--, B_count--;\n }\n } else {\n if (priority == \"ab\") {\n result += y * min(A_count, B_count);\n } else {\n result += x * min(A_count, B_count);\n }\n\n A_count = 0;\n B_count = 0;\n }\n }\n\n if (priority == \"ab\") {\n result += y * min(A_count, B_count);\n } else {\n result += x * min(A_count, B_count);\n }\n\n return result;\n }\n};",
"memory": "15909"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(const string& s, int x, int y) {\n ios::sync_with_stdio(false);\n int i = 0;\n int ans = 0;\n int begin = 0;\n bool is_section = false;\n while(i < s.size()){\n if(is_section){\n if(s[i] != 'a' && s[i] != 'b'){\n is_section = false;\n ans += solve_section(string(s.begin()+begin, s.begin()+i), x, y);\n }\n }\n else if(s[i] == 'a' || s[i] == 'b'){\n is_section = true;\n begin = i;\n }\n ++i;\n }\n if(is_section) ans += solve_section(string_view(s.begin()+begin, s.end()), x, y);\n return ans;\n }\nprivate:\n int solve_section(const string_view closed_section, int x, int y){\n if(x >= y){\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'a'){\n ++left_over_as;\n }\n else if(left_over_as != 0){\n --left_over_as;\n ans += x;\n }\n else{\n ++left_over_bs;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * y);\n }\n else{\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'b'){\n ++left_over_bs;\n }\n else if(left_over_bs != 0){\n --left_over_bs;\n ans += y;\n }\n else{\n ++left_over_as;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * x);\n }\n }\n};",
"memory": "16328"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(const string& s, int x, int y) {\n ios::sync_with_stdio(false);\n int i = 0;\n int ans = 0;\n int begin = 0;\n bool is_section = false;\n while(i < s.size()){\n if(is_section){\n if(s[i] != 'a' && s[i] != 'b'){\n is_section = false;\n ans += solve_section(string(s.begin()+begin, s.begin()+i), x, y);\n }\n }\n else if(s[i] == 'a' || s[i] == 'b'){\n is_section = true;\n begin = i;\n }\n ++i;\n }\n if(is_section) ans += solve_section(string_view(s.begin()+begin, s.end()), x, y);\n return ans;\n }\nprivate:\n int solve_section(string_view closed_section, int x, int y){\n if(x >= y){\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'a'){\n ++left_over_as;\n }\n else if(left_over_as != 0){\n --left_over_as;\n ans += x;\n }\n else{\n ++left_over_bs;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * y);\n }\n else{\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'b'){\n ++left_over_bs;\n }\n else if(left_over_bs != 0){\n --left_over_bs;\n ans += y;\n }\n else{\n ++left_over_as;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * x);\n }\n }\n};",
"memory": "16746"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(const string& s, int x, int y) {\n ios::sync_with_stdio(false);\n int i = 0;\n int ans = 0;\n int begin = 0;\n bool is_section = false;\n while(i < s.size()){\n if(is_section){\n if(s[i] != 'a' && s[i] != 'b'){\n is_section = false;\n ans += solve_section(string(s.begin()+begin, s.begin()+i), x, y);\n }\n }\n else if(s[i] == 'a' || s[i] == 'b'){\n is_section = true;\n begin = i;\n }\n ++i;\n }\n if(is_section) ans += solve_section(string_view(s.begin()+begin, s.end()), x, y);\n return ans;\n }\nprivate:\n int solve_section(const string_view& closed_section, int x, int y){\n if(x >= y){\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'a'){\n ++left_over_as;\n }\n else if(left_over_as != 0){\n --left_over_as;\n ans += x;\n }\n else{\n ++left_over_bs;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * y);\n }\n else{\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'b'){\n ++left_over_bs;\n }\n else if(left_over_bs != 0){\n --left_over_bs;\n ans += y;\n }\n else{\n ++left_over_as;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * x);\n }\n }\n};",
"memory": "16746"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int bCount = 0;\n int aCount = 0;\n int lesser = min(x, y);\n int ans = 0;\n \n for (auto &ch : s){\n if (ch == 'a'){\n if (y > x && bCount > 0){\n ans += y;\n bCount--;\n }\n else aCount++;\n }\n else if (ch == 'b'){\n if (x > y && aCount > 0){\n ans += x;\n aCount--;\n }\n else bCount++;\n }\n else {\n ans += min(aCount, bCount) * lesser;\n aCount = 0;\n bCount = 0;\n }\n }\n ans += min(aCount, bCount) * lesser;\n return ans;\n }\n};",
"memory": "17165"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int a = 0, b = 0, ans = 0;\n if(x>y){\n for(int i=0;i<s.length();i++){\n if(s[i]=='a')a++;\n else if(s[i]=='b'){\n if(a>0)\n ans+=x,a--;\n else b++;\n }\n else{\n if(a<=b)ans+=y*a;\n else ans+=y*b;\n a=0,b=0;\n }\n }\n if(a<=b)ans+=y*a;\n else ans+=y*b;\n } else {\n for(int i=0;i<s.length();i++){\n if(s[i]=='b')b++;\n else if(s[i]=='a'){\n if(b>0)\n ans+=y,b--;\n else a++;\n }\n else{\n if(b<=a)ans+=x*b;\n else ans+=x*a;\n a=0,b=0;\n }\n }\n if(b<=a)ans+=x*b;\n else ans+=x*a;\n }\n return ans;\n }\n};",
"memory": "17584"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int aCount = 0;\n int bCount = 0;\n int lesser = min(x, y);\n int result = 0;\n\n for (char c : s) {\n if (c > 'b') {\n result += min(aCount, bCount) * lesser;\n aCount = 0;\n bCount = 0;\n } else if (c == 'a') {\n if (x < y && bCount > 0) {\n bCount--;\n result += y;\n } else {\n aCount++;\n }\n } else {\n if (x > y && aCount > 0) {\n aCount--;\n result += x;\n } else {\n bCount++;\n }\n }\n }\n\n result += min(aCount, bCount) * lesser;\n return result;\n }\n};",
"memory": "18003"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int a = 0, b = 0;\n int score = 0;\n for(char &c : s){\n if(c == 'a'){\n if(x < y && b > 0){\n b--;\n score += y;\n }\n else a++;\n }\n else if(c == 'b'){\n if(x >= y && a > 0){\n a--;\n score += x;\n }\n else b++;\n }\n else{\n if(x < y) score += min(a, b) * x;\n else score += min(a, b) * y;\n a = b = 0;\n }\n // cout << score << \" \";\n }\n if(x < y) score += min(a, b) * x;\n else score += min(a, b) * y;\n return score;\n }\n};",
"memory": "18003"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n if(x>y){\n swap(x, y);\n reverse(s.begin(), s.end());\n }\n\n int n = s.size();\n int res = 0;\n int a = 0;\n int b = 0;\n for(int i=0; i<n; i++){\n if(s[i]!='a' && s[i]!='b'){\n res += min(a, b) * x;\n a = 0;\n b = 0;\n continue;\n }\n\n if(s[i]=='b') b++;\n else if(s[i]=='a'){\n if(b){\n res += y;\n b--;\n }\n else a++;\n }\n }\n res += min(a, b) * x;\n return res;\n }\n};",
"memory": "18421"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n stack<char>st;\n int c=0,d=0;\n for(auto &i:s){\n d++;\n if(i=='a'||i=='b'){\n if(x>=y){\n if(i=='b'){\n if(!st.empty()&&st.top()=='a'){\n c+=x;\n st.pop();\n }\n else{\n st.push(i);\n }\n }\n else{\n st.push(i);\n }\n \n }\n else{\n if(i=='a'){\n if(!st.empty()&&st.top()=='b'){\n c+=y;\n st.pop();\n }\n else{\n st.push(i);\n }\n }\n else{\n st.push(i);\n }\n }\n \n }\n else{\n int b=0,a=0;\n while(!st.empty()){\n char z=st.top();\n st.pop();\n if(z=='a'){\n if(b){\n c+=x;\n b--;\n }\n else{\n a++;\n }\n }\n else{\n if(a){\n c+=y;\n a--;\n }\n else\n {\n b++;\n }\n }\n }\n }\n }\n \n int b=0,a=0;\n while(!st.empty()){\n \n char z=st.top();\n\n st.pop();\n if(z=='a'){\n \n if(b){\n c+=x;\n b--;\n }\n else{\n a++;\n }\n }\n else{\n \n if(a){\n c+=y;\n a--;\n }\n else\n {\n b++;\n }\n }\n }\n cout<<c<<endl;\n \n return c;\n }\n\n};",
"memory": "18421"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int ans=0;\n int p=0;//ab->0 ba->1\n if(y>x) p=1;\n stack<char> st;\n int i=0;\n while(i<s.size()){\n if(st.size()==0 && (s[i]=='a' || s[i]=='b')){\n st.push(s[i]);\n }\n else if(st.size() && (s[i]=='a' || s[i]=='b')){\n if(!p && st.top()=='a' && s[i]=='b') {\n ans+=x;\n st.pop();\n }\n else if(p && st.top()=='b' && s[i]=='a'){\n ans+=y;\n st.pop();\n }\n else{\n st.push(s[i]);\n }\n }\n else{\n int a=0;\n int b=0;\n while(st.size()){\n if(st.top()=='a') a++;\n if(st.top()=='b') b++;\n st.pop();\n }\n if(p) ans+=(min(a,b)*x);\n else ans+=(min(a,b)*y);\n }\n i++;\n }\n int a=0;\n int b=0;\n while(st.size()){\n if(st.top()=='a') a++;\n if(st.top()=='b') b++;\n st.pop();\n }\n if(p) ans+=(min(a,b)*x);\n else ans+=(min(a,b)*y);\n return ans;\n \n }\n};",
"memory": "18840"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int abFirst(const string &s, int x, int y){\n int a = 0, b = 0, point = 0;\n for(int i=0;i<s.length();i++){\n if(s[i] == 'a'){\n a++;\n }else if(s[i] == 'b'){\n if(a == 0){\n b++;\n }else{\n point += x;\n a--;\n }\n }\n }\n point = point + min(a, b) * y;\n // cout<<s<<\", point = \"<<point<<'\\n';\n return point;\n }\n int maxGain(string &s, int x, int y){\n int point = abFirst(s, x, y);\n for(int i=0;i<s.length();i++){\n if(s[i] == 'a') s[i] = 'b';\n else s[i] = 'a';\n }\n point = max(point, abFirst(s, y, x));\n return point;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n int point = 0;\n string t = \"\";\n for(int i=0;i<s.length();i++){\n if(s[i] == 'a' || s[i] == 'b'){\n t += s[i];\n }else{\n point += maxGain(t, x, y);\n t = \"\";\n }\n }\n point += maxGain(t, x, y);\n return point;\n }\n};",
"memory": "18840"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int solve(string &s, int n, int i, int j, int cntA[], int cntB[], int x, int y) {\n if(j<=i) return 0;\n int used=0, ans=0, cnt=min(cntA[i]-cntA[j+1], cntB[i]-cntB[j+1]);\n int ca = cntA[i]-cntA[j+1];\n int cb = cntB[i]-cntB[j+1];\n if(x > y) {\n while(i<=j) {\n if(s[i] == 'a' && cb > 0) {\n // cout<<cb<<\" \"<<used<<endl;\n ans += x;\n used++;\n cb--;\n cnt--;\n }\n if(s[i] == 'b') {\n if(used>0) used--;\n else cb--;\n }\n i++;\n }\n return ans+cnt*y;\n }\n used=0;\n while(i<=j) {\n if(s[i] == 'b' && ca > 0) {\n ans += y;\n used++;\n ca--;\n cnt--;\n }\n if(s[i] == 'a') {\n if(used>0) used--;\n else ca--;\n }\n i++;\n }\n // cout<<ans<<\" x<y \"<<cnt<<endl;\n return ans+cnt*x;\n }\n int maximumGain(string s, int x, int y) {\n int n=s.size(), ans=0, last=-1;\n int cntA[n+1], cntB[n+1], ca=0, cb=0;\n cntA[n]=cntB[n]=0;\n for(int k=n-1;k>=0;k--) { //initialize count \n if(s[k]=='a') ca++;\n if(s[k]=='b') cb++;\n cntA[k] = ca;\n cntB[k] = cb;\n }\n\n for(int i=0;i<n;i++) {\n if(s[i] != 'a' && s[i] != 'b') {\n ans += solve(s, n, last+1, i-1, cntA, cntB, x, y);\n last = i;\n }\n }\n if(s[n-1] == 'a' || s[n-1] == 'b') {\n ans += solve(s, n, last+1, n-1, cntA, cntB, x, y);\n }\n return ans;\n }\n};",
"memory": "19259"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // greedy\n // time: O(n)\n // space: O(1)\n // b(ab)a: Assume score(ab) > score(ba) but removing ba first is optimal.\n // But after removing ab we can remove one ba, so we get score(ab) + score(ba) which is greater than score(ba) * 2.\n int maximumGain(string s, int x, int y) {\n string p1 = \"ab\", p2 = \"ba\";\n if (x < y) {\n swap(p1, p2);\n swap(x, y);\n }\n return helper(s, p1, x) + helper(s, p2, y);\n }\n\n int helper(string& s, string pattern, int score) {\n int index = 0;\n int res = 0;\n for (int i = 0; i < s.size(); ++i) {\n s[index++] = s[i];\n if (index > 1 && s.substr(index-2, 2) == pattern) {\n index -= 2;\n res += score;\n }\n }\n s.resize(index);\n return res;\n }\n};",
"memory": "19259"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n ios::sync_with_stdio(false);\n int i = 0;\n int ans = 0;\n int begin = 0;\n bool is_section = false;\n while(i < s.size()){\n if(is_section){\n if(s[i] != 'a' && s[i] != 'b'){\n is_section = false;\n ans += solve_section(string(s.begin()+begin, s.begin()+i), x, y);\n }\n }\n else if(s[i] == 'a' || s[i] == 'b'){\n is_section = true;\n begin = i;\n }\n ++i;\n }\n if(is_section) ans += solve_section(string_view(s.begin()+begin, s.end()), x, y);\n return ans;\n }\nprivate:\n int solve_section(string_view closed_section, int x, int y){\n if(x >= y){\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'a'){\n ++left_over_as;\n }\n else if(left_over_as != 0){\n --left_over_as;\n ans += x;\n }\n else{\n ++left_over_bs;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * y);\n }\n else{\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'b'){\n ++left_over_bs;\n }\n else if(left_over_bs != 0){\n --left_over_bs;\n ans += y;\n }\n else{\n ++left_over_as;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * x);\n }\n }\n};",
"memory": "19678"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int i = 0;\n int ans = 0;\n int begin = 0;\n bool is_section = false;\n while(i < s.size()){\n if(is_section){\n if(s[i] != 'a' && s[i] != 'b'){\n is_section = false;\n ans += solve_section(string(s.begin()+begin, s.begin()+i), x, y);\n }\n }\n else if(s[i] == 'a' || s[i] == 'b'){\n is_section = true;\n begin = i;\n }\n ++i;\n }\n if(is_section) ans += solve_section(string(s.begin()+begin, s.end()), x, y);\n return ans;\n }\nprivate:\n int solve_section(string&& closed_section, int x, int y){\n if(x >= y){\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'a'){\n ++left_over_as;\n }\n else if(left_over_as != 0){\n --left_over_as;\n ans += x;\n }\n else{\n ++left_over_bs;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * y);\n }\n else{\n int left_over_as = 0;\n int left_over_bs = 0;\n int ans = 0;\n for(int i = 0; i < closed_section.size(); ++i){\n if(closed_section[i] == 'b'){\n ++left_over_bs;\n }\n else if(left_over_bs != 0){\n --left_over_bs;\n ans += y;\n }\n else{\n ++left_over_as;\n }\n }\n return ans + (min(left_over_as, left_over_bs) * x);\n }\n }\n};",
"memory": "19678"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int points1 = 0, points2 = 0;\n stack<char> st;\n for (char i : s) {\n if (i == 'b')\n st.push(i);\n else if (i == 'a') {\n if (!st.empty() && st.top() == 'b') {\n st.pop();\n points1 += y;\n } else if (st.empty() || st.top() == 'a')\n st.push('a');\n } else {\n int a1 = 0;\n int b1 = 0;\n while (!st.empty()) {\n if (st.top() == 'b')\n b1++;\n else\n a1++;\n st.pop();\n }\n points1 += min(a1, b1) * x;\n }\n }\n int a = 0, b = 0;\n while (!st.empty()) {\n if (st.top() == 'a')\n a++;\n else\n b++;\n st.pop();\n }\n points1 += min(b, a) * x;\n for (char i : s) {\n if (i == 'a')\n st.push(i);\n else if (i == 'b') {\n if (!st.empty() && st.top() == 'a') {\n st.pop();\n points2 += x;\n } else if (st.empty() || st.top() == 'b')\n st.push('b');\n\n } else {\n int a1 = 0;\n int b1 = 0;\n while (!st.empty()) {\n if (st.top() == 'b')\n b1++;\n else\n a1++;\n st.pop();\n }\n points2 += min(a1, b1) * y;\n }\n }\n a = 0, b = 0;\n while (!st.empty()) {\n if (st.top() == 'b')\n b++;\n else\n a++;\n st.pop();\n }\n points2 += min(b, a) * y;\n cout << points1 << \" \" << points2 << endl;\n return max(points1, points2);\n }\n};",
"memory": "20096"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int points1 = 0, points2 = 0;\n stack<char> st;\n for (char i : s) {\n if (i == 'b')\n st.push(i);\n else if (i == 'a') {\n if (!st.empty() && st.top() == 'b') {\n st.pop();\n points1 += y;\n } else if (st.empty() || st.top() == 'a')\n st.push('a');\n } else {\n int a1 = 0;\n int b1 = 0;\n while (!st.empty()) {\n if (st.top() == 'b')\n b1++;\n else\n a1++;\n st.pop();\n }\n points1 += min(a1, b1) * x;\n }\n }\n int a = 0, b = 0;\n while (!st.empty()) {\n if (st.top() == 'a')\n a++;\n else\n b++;\n st.pop();\n }\n points1 += min(b, a) * x;\n for (char i : s) {\n if (i == 'a')\n st.push(i);\n else if (i == 'b') {\n if (!st.empty() && st.top() == 'a') {\n st.pop();\n points2 += x;\n } else if (st.empty() || st.top() == 'b')\n st.push('b');\n\n } else {\n int a1 = 0;\n int b1 = 0;\n while (!st.empty()) {\n if (st.top() == 'b')\n b1++;\n else\n a1++;\n st.pop();\n }\n points2 += min(a1, b1) * y;\n }\n }\n a = 0, b = 0;\n while (!st.empty()) {\n if (st.top() == 'b')\n b++;\n else\n a++;\n st.pop();\n }\n points2 += min(b, a) * y;\n cout << points1 << \" \" << points2 << endl;\n return max(points1, points2);\n }\n};",
"memory": "20096"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int solve(string &s,string p,int point){\n stack<char>st;\n int total=0;\n for(int i=0;i<s.size();i++){\n if(st.empty()){\n st.push(s[i]);\n }\n else{\n if(s[i]==p[1]&&st.top()==p[0]){\n st.pop();\n total+=point;\n }\n else{\n st.push(s[i]);\n }\n }\n }\n s=\"\";\n while(!st.empty()){\n s+=st.top();\n st.pop();\n }\n reverse(s.begin(),s.end());\n return total;\n \n }\n int maximumGain(string s, int x, int y) {\n string s1=\"ab\";\n string s2=\"ba\";\n if(x<y){\n swap(s1,s2);\n swap(x,y);\n }\n return solve(s,s1,x)+solve(s,s2,y);\n }\n};",
"memory": "23028"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int ans = 0;\n if( x > y)\n {\n stack<char>st;\n for(int i=0;i<s.size();i++)\n {\n if(st.empty() == 1 || (s[i] != 'a' && s[i] != 'b'))\n st.push(s[i]);\n else if(s[i] == 'b' && st.top() == 'a')\n {\n ans= ans+x;\n st.pop();\n }\n else\n {\n st.push(s[i]);\n }\n }\n stack<char>temp;\n while(st.empty() != 1)\n {\n if(temp.empty() == 1)\n temp.push(st.top());\n else if(st.top() == 'b' && temp.top() == 'a')\n {\n ans= ans+y;\n temp.pop();\n }\n else\n temp.push(st.top());\n st.pop();\n }\n }\n else\n {\n stack<char>st;\n for(int i=0;i<s.size();i++)\n {\n if(st.empty() == 1 || (s[i] != 'a' && s[i] != 'b'))\n st.push(s[i]);\n else if(s[i] == 'a' && st.top() == 'b')\n {\n ans= ans+y;\n st.pop();\n }\n else\n {\n st.push(s[i]);\n }\n }\n stack<char>temp;\n while(st.empty() != 1)\n {\n if(temp.empty() == 1)\n temp.push(st.top());\n else if(st.top() == 'a' && temp.top() == 'b')\n {\n ans= ans+x;\n temp.pop();\n }\n else\n temp.push(st.top());\n st.pop();\n }\n }\n return ans;\n }\n};",
"memory": "23028"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n void getCount(string str, string sub, int& cnt1, int& cnt2) {\n \n char first = sub[0], second = sub[1];\n int i = 1;\n while(i < str.length()) {\n if(i > 0 && str[i-1] == first && str[i] == second) {\n cnt1++;\n str.erase(i-1, 2);\n i--;\n continue;\n }\n i++;\n }\n\n i = 1;\n while(i < str.length()) {\n if(i > 0 && str[i-1] == second && str[i] == first) {\n cnt2++;\n str.erase(i-1, 2);\n i--;\n continue;\n }\n i++;\n }\n return;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n \n int mxABcnt = 0;\n int mxBAcnt = 0;\n int minBAcnt = 0;\n int minABcnt= 0;\n\n getCount(s, \"ab\", mxABcnt, minBAcnt);\n getCount(s, \"ba\", mxBAcnt, minABcnt);\n\n int operation1 = mxABcnt * x + minBAcnt * y;\n int operation2 = mxBAcnt * y + minABcnt * x;\n return max(operation1, operation2);\n }\n};",
"memory": "23446"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n void getCount(string str, string sub, int& cnt1, int& cnt2) {\n \n char first = sub[0], second = sub[1];\n int i = 1;\n while(i < str.length()) {\n if(i > 0 && str[i-1] == first && str[i] == second) {\n cnt1++;\n str.erase(i-1, 2);\n i--;\n continue;\n }\n i++;\n }\n\n i = 1;\n while(i < str.length()) {\n if(i > 0 && str[i-1] == second && str[i] == first) {\n cnt2++;\n str.erase(i-1, 2);\n i--;\n continue;\n }\n i++;\n }\n return;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n \n int mxABcnt = 0;\n int mxBAcnt = 0;\n int minBAcnt = 0;\n int minABcnt= 0;\n\n getCount(s, \"ab\", mxABcnt, minBAcnt);\n getCount(s, \"ba\", mxBAcnt, minABcnt);\n\n int operation1 = mxABcnt * x + minBAcnt * y;\n int operation2 = mxBAcnt * y + minABcnt * x;\n return max(operation1, operation2);\n }\n};",
"memory": "23446"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int ans = 0;\n stack<char> stk;\n string firstPat=\"ba\",secondPat=\"ab\";\n if(x>y){\n firstPat=\"ab\";\n secondPat=\"ba\";\n }\n\n int n = s.size();\n int i=0;\n\n while(i<n){\n if(stk.size()<1){\n stk.push(s[i]);\n }\n else{\n string pattern=\"\";\n pattern.push_back(stk.top());\n pattern.push_back(s[i]);\n\n if(pattern == firstPat){\n stk.pop();\n ans += (max(x,y));\n }\n else{\n stk.push(s[i]);\n }\n }\n i++;\n }\n\n //now resolve the other pattern\n stack<char> stk2;\n \n while(!stk.empty()){\n char ele = stk.top();\n stk.pop();\n if(stk2.size()<1){\n stk2.push(ele);\n }\n else{\n string pattern = \"\";\n pattern.push_back(ele);\n pattern.push_back(stk2.top());\n if(pattern == secondPat){\n stk2.pop();\n ans += (min(x,y));\n }\n else{\n stk2.push(ele);\n }\n }\n }\n return ans;\n }\n};",
"memory": "23865"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int res = 0;\n string cur;\n if (x >= y){\n for (auto&l: s){\n cur.push_back(l);\n if (cur.length() >= 2 && cur.substr(cur.length()-2) == \"ab\"){\n cur.pop_back(); cur.pop_back();\n res += x;\n } \n }\n //cout << s << \" \" << cur << endl; \n s = cur; cur = \"\";\n for (auto&l: s){\n cur.push_back(l);\n if (cur.length() >= 2 && cur.substr(cur.length()-2) == \"ba\"){\n cur.pop_back(); cur.pop_back();\n res += y;\n } \n }\n }\n else{\n for (auto&l: s){\n cur.push_back(l);\n if (cur.length() >= 2 && cur.substr(cur.length()-2) == \"ba\"){\n cur.pop_back(); cur.pop_back();\n res += y;\n } \n }\n s = cur; cur = \"\";\n\n for (auto&l: s){\n cur.push_back(l);\n if (cur.length() >= 2 && cur.substr(cur.length()-2) == \"ab\"){\n cur.pop_back(); cur.pop_back();\n res += x;\n } \n }\n }\n return res;\n }\n};",
"memory": "23865"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n if (x < y) {\n reverse(s.begin(), s.end());\n return maximumGain(s, y, x);\n }\n int ans = 0;\n stack<char> stk1;\n stack<char> stk2;\n for (char c : s) {\n if (c != 'b')\n stk1.push(c);\n else {\n if (!stk1.empty() && stk1.top() == 'a') {\n stk1.pop();\n ans += x;\n } else\n stk1.push(c);\n }\n }\n while (!stk1.empty()) {\n char c = stk1.top();\n stk1.pop();\n if (c != 'b')\n stk2.push(c);\n else {\n if (!stk2.empty() && stk2.top() == 'a') {\n stk2.pop();\n ans += y;\n } else\n stk2.push(c);\n }\n }\n return ans;\n }\n};",
"memory": "24284"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n // Function to calculate the maximum gain by removing \"ab\" or \"ba\" substrings\n int maximumGain(string s, int x, int y) {\n // If x is less than y, reverse the string and swap values of x and y to \n // prioritize removing \"ba\" before \"ab\" since we want to maximize the gain\n if (x < y) {\n reverse(s.begin(), s.end());\n return maximumGain(s, y, x);\n }\n // Variable to store the total gain\n int totalGain = 0;\n stack<char> stkAb; // Stack to manage \"ab\" pairs\n stack<char> stkBa; // Stack to manage \"ba\" pairs\n\n // Iterate through the characters of the string\n for (char c : s) {\n // If the current character is 'a' or it's not 'b', push it onto stkAb\n if (c != 'b')\n stkAb.push(c);\n else {\n // If the top of stkAb is 'a', we found \"ab\" so we pop 'a' and \n // add x to totalGain\n if (!stkAb.empty() && stkAb.top() == 'a') {\n stkAb.pop();\n totalGain += x;\n } else {\n // Otherwise, push 'b' to stkAb\n stkAb.push(c);\n }\n }\n }\n\n // While stkAb is not empty, we will manage \"ba\" pairs\n while (!stkAb.empty()) {\n char c = stkAb.top();\n stkAb.pop();\n // If the current character is 'a' or it's not 'b', push it onto stkBa\n if (c != 'b')\n stkBa.push(c);\n else {\n // If the top of stkBa is 'a', we found \"ba\" so we pop 'a' and \n // add y to totalGain\n if (!stkBa.empty() && stkBa.top() == 'a') {\n stkBa.pop();\n totalGain += y;\n } else {\n // Otherwise, push 'b' to stkBa\n stkBa.push(c);\n }\n }\n }\n\n // Return the calculated total gain\n return totalGain;\n }\n};\n",
"memory": "24284"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n stack<char> st1, st2;\n int ans = 0;\n \n // First pass: remove \"ab\" or \"ba\" based on the higher value\n if (x >= y) {\n for (char c : s) {\n if (!st1.empty() && st1.top() == 'a' && c == 'b') {\n ans += x;\n st1.pop();\n } else {\n st1.push(c);\n }\n }\n } else {\n for (char c : s) {\n if (!st1.empty() && st1.top() == 'b' && c == 'a') {\n ans += y;\n st1.pop();\n } else {\n st1.push(c);\n }\n }\n }\n \n // Transfer the remaining elements to another stack for the second pass\n while (!st1.empty()) {\n st2.push(st1.top());\n st1.pop();\n }\n \n // Second pass: remove the remaining pairs\n if (x >= y) {\n while (!st2.empty()) {\n char c = st2.top();\n st2.pop();\n if (!st1.empty() && st1.top() == 'b' && c == 'a') {\n ans += y;\n st1.pop();\n } else {\n st1.push(c);\n }\n }\n } else {\n while (!st2.empty()) {\n char c = st2.top();\n st2.pop();\n if (!st1.empty() && st1.top() == 'a' && c == 'b') {\n ans += x;\n st1.pop();\n } else {\n st1.push(c);\n }\n }\n }\n \n return ans;\n }\n};\n",
"memory": "24703"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string &s, int &x, int &y) {\n int n = s.size();\n stack<char>st;\n\n bool flag = y>x;\n string targetPair = flag ? \"ba\" : \"ab\";\n int sum =0;\n for(int i =0; i<s.size();i++){\n if(!st.empty() && targetPair[1]==s[i] && st.top()==targetPair[0]){\n st.pop();\n sum+= max(x, y);\n }\n else st.push(s[i]);\n }\n\n string newString= \"\";\n while(!st.empty()){\n newString.push_back(st.top());\n st.pop();\n }\n reverse(newString.begin(), newString.end());\n\n flag = !flag;\n targetPair = flag ? \"ba\" : \"ab\";\n for(int i =0; i<newString.size();i++){\n if(!st.empty() && targetPair[1]==newString[i] && st.top()==targetPair[0]){\n st.pop();\n sum+= min(x, y);\n }\n else st.push(newString[i]);\n }\n\n return sum;\n }\n};",
"memory": "24703"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int score =0;\n string temp=s;\n if(x>y){\n int i=-1;\n int j=0;\n string temp=s;\n while(j<s.size()){\n if(i>=0 && temp[i] == 'a' && s[j] == 'b'){\n score += x;\n i--;\n j++;\n } else{\n cout<<\"hi\"<<endl;\n i++;\n temp[i] = s[j++];\n }\n }\n\n s = temp.substr(0,i+1);\n temp = s;\n i=-1;\n j=0;\n while(j<s.size()){\n if(i>=0 && temp[i] == 'b' && s[j] == 'a'){\n score += y;\n i--;\n j++;\n } else{\n i++;\n temp[i] = s[j++];\n }\n } \n } else{\n int i=-1;\n int j=0;\n string temp = s;\n while(j<s.size()){\n if(i>=0 && temp[i] == 'b' && s[j] == 'a'){\n score += y;\n i--;\n j++;\n } else{\n temp[++i] = s[j++];\n }\n }\n\n s = temp.substr(0,i+1); \n temp = s;\n \n i=-1;\n j=0;\n while(j<s.size()){\n if(i>=0 && temp[i] == 'a' && s[j] == 'b'){\n score += x;\n i--;\n j++;\n } else{\n temp[++i] = s[j++];\n }\n } \n } \n\n return score;\n }\n};",
"memory": "25121"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "\n\n\nclass Solution {\npublic:\n int solvey(stack<char>& st, int points) {\n int cnt = 0;\n stack<char> tempStack;\n \n \n while (!st.empty()) {\n tempStack.push(st.top());\n st.pop();\n }\n \n \n stack<char> reverseStack;\n while (!tempStack.empty()) {\n char top = tempStack.top();\n tempStack.pop();\n if (!reverseStack.empty() && reverseStack.top() == 'a' && top == 'b') {\n cnt += points;\n reverseStack.pop();\n } else {\n reverseStack.push(top);\n }\n }\n \n return cnt;\n }\n\n int removeAndCounty(string& s, int y,int x) {\n int cnt = 0;\n stack<char> st;\n \n for (char ch : s) {\n if (!st.empty() && st.top() == 'b' && ch == 'a') {\n cnt += y;\n st.pop();\n } else {\n st.push(ch);\n }\n }\n\n cnt += solvey(st, x);\n \n return cnt;\n }\n\n int solve(stack<char>& st, int points) {\n int cnt = 0;\n stack<char> tempStack;\n \n \n while (!st.empty()) {\n tempStack.push(st.top());\n st.pop();\n }\n \n \n stack<char> reverseStack;\n while (!tempStack.empty()) {\n char top = tempStack.top();\n tempStack.pop();\n if (!reverseStack.empty() && reverseStack.top() == 'b' && top == 'a') {\n cnt += points;\n reverseStack.pop();\n } else {\n reverseStack.push(top);\n }\n }\n \n return cnt;\n }\n\n int removeAndCount(string& s, int x,int y) {\n int cnt = 0;\n stack<char> st;\n \n for (char ch : s) {\n if (!st.empty() && st.top() == 'a' && ch == 'b') {\n cnt += x;\n st.pop();\n } else {\n st.push(ch);\n }\n }\n\n cnt += solve(st, y);\n \n return cnt;\n }\n\n int maximumGain(string s, int x, int y) {\n int totalPoints = 0;\n \n if (x >= y) {\n totalPoints += removeAndCount(s, x,y);\n } else {\n totalPoints += removeAndCounty(s, y,x);\n }\n \n return totalPoints;\n }};\n\n\n\n\n\n\n",
"memory": "25121"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "\nclass Solution {\npublic:\n int solve(string s, int y, string k, int z) {\n int points = 0;\n string x =\"\";\n\n for (int i = 0; i < s.size(); i++) {\n if (x.size() && s[i] == k[1] && x[x.size()-1]== k[0]) {\n points += y;\n x.pop_back();\n } else {\n x.push_back(s[i]);\n }\n }\n\n \n \n\n s=\"\";\n for (int i = 0; i < x.size(); i++) {\n if (s.size() && x[i] == k[0] && s[s.size()-1] == k[1]) {\n points += z;\n s.pop_back();\n } else {\n s.push_back(x[i]);\n }\n }\n\n return points;\n }\n\n int maximumGain(string s, int x, int y) {\n string k = \"ba\";\n string S = \"ab\";\n if (y > x) {\n return solve(s, y, k, x);\n }\n return solve(s, x, S, y);\n }\n};\n",
"memory": "25540"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n if (y > x) {\n swap(x, y);\n reverse(s.begin(), s.end());\n }\n int n = s.size();\n int ans = 0;\n string st;\n for (int i=0; i<n; ++i) {\n if (s[i] == 'b') {\n if (st.size() > 0 && st.back() == 'a') {\n st.pop_back();\n ans += x;\n }else\n st.push_back(s[i]);\n }else\n st.push_back(s[i]);\n }\n\n // cout << st << endl;\n string w;\n for (int i=0; i<st.size(); ++i) {\n if (st[i] == 'a') {\n if (w.size() > 0 && w.back() == 'b') {\n w.pop_back();\n ans += y;\n }else\n w.push_back(st[i]);\n }else\n w.push_back(st[i]);\n // cout << \"i: \" << i << \" , w: \" << w << endl;\n }\n\n return ans;\n }\n};\n/*\ncdbcbbaaabab\nbaabcbdc\n*/",
"memory": "25540"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int removeAB(string& s, int add) {\n int sum = 0;\n stack<char> st;\n for(auto ch: s) {\n st.push(ch);\n if(st.top() == 'b') {\n st.pop();\n if(!st.empty() && st.top() == 'a') {\n st.pop();\n sum += add;\n } else {\n st.push('b');\n }\n }\n }\n s = \"\";\n while(!st.empty()) {\n s += st.top();\n st.pop();\n }\n reverse(s.begin(), s.end());\n //cout<<sum <<\" ab \"<<s<<endl;\n return sum;\n }\n int removeBA(string& s, int add) {\n int sum = 0;\n stack<char> st;\n for(auto ch: s) {\n st.push(ch);\n if(st.top() == 'a') {\n st.pop();\n if(!st.empty() && st.top() == 'b') {\n st.pop();\n sum += add;\n } else {\n st.push('a');\n }\n }\n }\n s = \"\";\n while(!st.empty()) {\n s += st.top();\n st.pop();\n }\n \n reverse(s.begin(), s.end());\n //cout<<sum <<\" ba \"<<s<<endl;\n return sum;\n }\n int maximumGain(string s, int x, int y) {\n int ans = 0;\n\n if(x > y) {\n return removeAB(s, x) + removeBA(s, y);\n }\n\n return removeBA(s, y) + removeAB(s, x);\n }\n};",
"memory": "25959"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n\npublic:\n int maximumGain(string s, int x, int y) {\n int points=0;;\n\n auto removePairs = [&](char first, char second, int point){\n string newString;\n int tempPoints=0;\n for(char ch : s){\n if(!newString.empty() && newString.back()==first && ch==second){\n tempPoints+=point;\n newString.pop_back();\n }else{\n newString.push_back(ch);\n }\n }\n s=newString;\n return tempPoints;\n };\n if(x>y){\n points+=removePairs('a','b',x);\n points+=removePairs('b','a',y);\n }else{\n points+=removePairs('b','a',y);\n points+=removePairs('a','b',x);\n }\n return points;\n }\n};",
"memory": "25959"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int ans=0;\n // getba(s,y);\n if(x>y){\n ans+=getab(s,x)+getba(s,y);\n }else{\n ans+=getba(s,y)+getab(s,x);\n }\n return ans;\n }\n int getba(string &s,int y){\n string t;\n string bb;\n int n=s.size();\n int score=0;\n for(int i=0;i<n;i++){\n if(s[i]=='b'){\n bb.push_back('b');\n }\n else if(s[i]=='a'){\n if(bb.size()>0){\n score+=y;\n bb.pop_back();\n }\n else{\n t+=s[i];\n }\n }else{\n t+=bb;\n bb=\"\";\n t+=s[i];\n }\n }\n t+=bb;\n s=t;\n cout<<s<<endl;\n return score;\n }\n int getab(string &s,int x){\n string t;\n string bb;\n int n=s.size();\n int score=0;\n for(int i=0;i<n;i++){\n if(s[i]=='a'){\n bb.push_back('a');\n }\n else if(s[i]=='b'){\n if(bb.size()>0){\n score+=x;\n bb.pop_back();\n }\n else{\n t+=s[i];\n }\n }else{\n\n t+=bb;\n bb=\"\";\n t+=s[i];\n }\n }\n t+=bb;\n s=t;\n return score;\n }\n};",
"memory": "26378"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n string s1=\"\" , s2=\"\";\n int ans=0;\n if(x>=y) {\n s1=\"ab\";\n s2=\"ba\";\n }\n else {\n s2=\"ab\";\n s1=\"ba\";\n }\n int top1=0;\n stack <char> st1, st2;\n for(int i=0; i<s.length(); i++){\n if(s[i]==s1[1]){\n if(top1>=1){\n if(st1.top()==s1[0]){\n st1.pop();\n top1--;\n ans+=max(x,y);\n }\n else {\n st1.push(s[i]);\n top1++;\n }\n }\n else{\n st1.push(s[i]);\n top1++;\n }\n }\n else {\n st1.push(s[i]);\n top1++;\n }\n }\n while(!st1.empty()){\n st2.push(st1.top());\n st1.pop();\n }\n s=\"\";\n while(!st2.empty()){\n s+=st2.top();\n st2.pop();\n }\n top1=0;\n s1=s2;\n for(int i=0; i<s.length(); i++){\n if(s[i]==s1[1]){\n if(top1>=1){\n if(st1.top()==s1[0]){\n st1.pop();\n top1--;\n ans+=min(x,y);\n }\n else {\n st1.push(s[i]);\n top1++;\n }\n }\n else{\n st1.push(s[i]);\n top1++;\n }\n }\n else {\n st1.push(s[i]);\n top1++;\n }\n }\n while(!st1.empty()){\n st2.push(st1.top());\n st1.pop();\n }\n s=\"\";\n while(!st2.empty()){\n s+=st2.top();\n st2.pop();\n }\n return ans;\n }\n};",
"memory": "26378"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int res = 0;\n string top, bot;\n int top_score, bot_score;\n\n if (y > x) {\n top = \"ba\";\n top_score = y;\n bot = \"ab\";\n bot_score = x;\n } else {\n top = \"ab\";\n top_score = x;\n bot = \"ba\";\n bot_score = y;\n } \n // Removing first top substrings cause they give more points\n vector<char> stack;\n for (char ch : s) { // Changed 'char' to 'ch'\n if (ch == top[1] && !stack.empty() && stack.back() == top[0]) {\n res += top_score;\n stack.pop_back();\n } else {\n stack.push_back(ch);\n }\n }\n\n // Removing bot substrings cause they give less or equal amount of scores\n vector<char> new_stack;\n for (char ch : stack) { // Changed 'char' to 'ch'\n if (ch == bot[1] && !new_stack.empty() && new_stack.back() == bot[0]) {\n res += bot_score;\n new_stack.pop_back();\n } else {\n new_stack.push_back(ch);\n }\n }\n\n return res; \n }\n};",
"memory": "26796"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n \n vector<char>st;\n string check = \"\";\n string next_check = \"\";\n\n int add = 0;\n int next_add = 0;\n if(x>y){\n \n check = \"ab\";\n next_check = \"ba\";\n add = x;\n next_add = y;\n }\n else{\n check = \"ba\";\n next_check=\"ab\";\n add = y;\n next_add = x;\n }\n int ans=0;\n for(auto i:s){\n \n if(i==check[1] && !st.empty() && st.back() == check[0]){\n ans+=add;\n st.pop_back();\n \n }\n else\n st.push_back(i);\n }\n\n vector<char>new_s;\n \n for(auto i:st){\n \n if(i==next_check[1] && !new_s.empty() && new_s.back() == next_check[0] ){\n\n ans+=next_add;\n new_s.pop_back();\n }\n else\n new_s.push_back(i);\n }\n\n return ans;\n\n }\n};",
"memory": "26796"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n \n if (x < y) {\n swap(x, y);\n for (auto& c : s) {\n if (c == 'a') c = 'b';\n else if (c == 'b') c = 'a';\n }\n }\n\n int maxPoints = 0;\n stack<char> st;\n\n \n for (char c : s) {\n if (!st.empty() && st.top() == 'a' && c == 'b') {\n maxPoints += x; \n st.pop(); \n } else {\n st.push(c);\n }\n }\n\n string remaining;\n while (!st.empty()) {\n remaining.push_back(st.top());\n st.pop();\n }\n\n reverse(remaining.begin(), remaining.end());\n\n for (char c : remaining) {\n if (!st.empty() && st.top() == 'b' && c == 'a') {\n maxPoints += y; \n st.pop(); \n } else {\n st.push(c);\n }\n }\n\n return maxPoints;\n }\n};\n",
"memory": "27215"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution{\npublic:\n int maximumGain(string s, int x, int y) {\n if (x < y) { \n swap(x, y);\n for (char& c : s) {\n if (c == 'a'){\n c = 'b';\n }\n \n else if (c == 'b'){\n c = 'a';\n }\n }\n }\n stack<char> st;\n int max_points = 0;\n\n for (char c : s) {\n if (!st.empty() && st.top() == 'a' && c == 'b') {\n st.pop();\n max_points += x;\n } else {\n st.push(c);\n }\n }\n\n string remaining;\n while (!st.empty()) {\n remaining += st.top();\n st.pop();\n }\n reverse(remaining.begin(), remaining.end());\n\n for (char c : remaining) {\n if (!st.empty() && st.top() == 'b' && c == 'a') {\n st.pop();\n max_points += y;\n } else {\n st.push(c);\n \n\n }\n }\n return max_points;\n \n}\n};\n \n \n \n\n \n\n\n \n \n\n\n\n",
"memory": "27215"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n vector<char> tmp;\n if (x > y) {\n for (char c: s) {\n if (c == 'a') c = 'b';\n else if (c == 'b') c = 'a';\n tmp.push_back(c);\n }\n swap(x, y);\n } else {\n tmp.insert(tmp.begin(), s.begin(), s.end());\n }\n stack<char> st;\n int score = 0;\n for (char c: tmp) {\n if (c == 'a' && st.size() > 0 && st.top() == 'b') {\n st.pop();\n score += y;\n } else {\n st.push(c);\n }\n }\n stack<char> st2;\n while (st.size() > 0) {\n char c = st.top();\n st.pop();\n if (c == 'a' && st2.size() > 0 && st2.top() == 'b') {\n score += x;\n st2.pop();\n } else {\n st2.push(c);\n }\n }\n return score;\n }\n};",
"memory": "27634"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n std::string expensive, cheap;\n\n if(x >= y){\n expensive = \"ab\";\n cheap = \"ba\";\n }\n else{\n expensive = \"ba\";\n cheap = \"ab\";\n swap(x,y);\n }\n\n int res = 0;\n stack<char> temp1;\n for(char c: s){\n if(!temp1.empty() && temp1.top() == expensive[0] && c == expensive[1]){\n res += x;\n temp1.pop();\n }\n else{\n temp1.push(c);\n }\n }\n\n vector<char> remaining;\n while (!temp1.empty()) {\n remaining.push_back(temp1.top());\n temp1.pop();\n }\n\n stack<char> temp2;\n // 逆序遍历vector以模拟原始栈顺序\n for (auto it = remaining.rbegin(); it != remaining.rend(); ++it) {\n char c = *it;\n if (!temp2.empty() && temp2.top() == cheap[0] && c == cheap[1]) {\n res += y;\n temp2.pop();\n } else {\n temp2.push(c);\n }\n }\n return res;\n }\n};\n\n\n",
"memory": "27634"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n vector<char> vec;\n int top_score = 0, bottom_score = 0, ans = 0;\n string firstRem, secondRem;\n\n if (x > y) {\n firstRem = \"ab\";\n secondRem = \"ba\";\n top_score = x;\n bottom_score = y;\n } else {\n firstRem = \"ba\";\n secondRem = \"ab\";\n top_score = y;\n bottom_score = x;\n }\n\n for (char c : s) {\n if (!vec.empty() && vec.back() == firstRem[0] && c == firstRem[1]) {\n ans += top_score;\n vec.pop_back();\n } else {\n vec.push_back(c);\n }\n }\n \n string remaining = \"\";\n for (char c : vec) remaining += c;\n vec.clear();\n\n for (char c : remaining) {\n if (!vec.empty() && vec.back() == secondRem[0] && c == secondRem[1]) {\n ans += bottom_score;\n vec.pop_back();\n } else {\n vec.push_back(c);\n }\n }\n return ans;\n }\n};\n",
"memory": "28053"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "static const int _ = [] {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n return 0;\n}();\n\n\nclass Solution {\npublic:\n int maximumGain(string &s, int x, int y) {\n int totalScore = 0;\n string highPriorityPair = x > y ? \"ab\" : \"ba\";\n string lowPriorityPair = x <= y ? \"ab\" : \"ba\";\n\n string stringAfterFirstPass = removeSubstring(s, highPriorityPair);\n int removedPairsCount = (s.length() - stringAfterFirstPass.length()) / 2;\n totalScore += removedPairsCount * max(x, y);\n\n string stringAfterSecondPass = removeSubstring(stringAfterFirstPass, lowPriorityPair);\n removedPairsCount = (stringAfterFirstPass.length() - stringAfterSecondPass.length()) / 2;\n totalScore += removedPairsCount * min(x, y);\n\n return totalScore;\n }\n\nprivate:\n string removeSubstring(const string &input, const string &targetPair) {\n stack<char> charStack;\n\n for (char currentChar: input) {\n if (currentChar == targetPair[1] && !charStack.empty() && charStack.top() == targetPair[0]) {\n charStack.pop(); // Remove the matching character from the stack\n } else {\n charStack.push(currentChar);\n }\n }\n\n string remainingChars;\n while (!charStack.empty()) {\n remainingChars += charStack.top();\n charStack.pop();\n }\n reverse(remainingChars.begin(), remainingChars.end());\n \n return remainingChars;\n }\n};",
"memory": "28053"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "#include <iostream>\n#include <string>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n if (y > x) {\n return helper(s, y, x, 'b', 'a');\n } else {\n return helper(s, x, y, 'a', 'b');\n }\n }\n\n int helper(string s, int high, int low, char first, char second) {\n string sb;\n int total = 0;\n\n \n for (char c : s) {\n if (!sb.empty() && sb.back() == first && c == second) {\n sb.pop_back(); \n total += high;\n } else {\n sb.push_back(c);\n }\n }\n\n \n string result;\n for (char c : sb) {\n if (!result.empty() && result.back() == second && c == first) {\n result.pop_back();\n total += low;\n } else {\n result.push_back(c);\n }\n }\n\n return total;\n }\n};\n\n",
"memory": "28471"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "#include <iostream>\n#include <string>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n if (y > x) {\n return helper(s, y, x, 'b', 'a');\n } else {\n return helper(s, x, y, 'a', 'b');\n }\n }\n\n int helper(string s, int high, int low, char first, char second) {\n string sb;\n int total = 0;\n\n \n for (char c : s) {\n if (!sb.empty() && sb.back() == first && c == second) {\n sb.pop_back(); \n total += high;\n } else {\n sb.push_back(c);\n }\n }\n\n \n string result;\n for (char c : sb) {\n if (!result.empty() && result.back() == second && c == first) {\n result.pop_back();\n total += low;\n } else {\n result.push_back(c);\n }\n }\n\n return total;\n }\n};\n\n",
"memory": "28471"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int ans = 0;\n vector < char > c;\n for (int i = 0; i < s.size(); i++) {\n string cur = \"\";\n cur += s[i];\n if (c.size() > 0) {\n cur += c.back();\n if (x < y && cur == \"ab\") {\n c.pop_back(); ans += y; continue;\n } else if (x >= y && cur == \"ba\") {\n c.pop_back(); ans += x; continue;\n }\n }\n c.push_back(s[i]);\n }\n string ss = \"\";\n for (int i = 0; i < c.size(); i++) ss += c[i];\n c.clear();\n for (int i = 0; i < ss.size(); i++) {\n string cur = \"\";\n cur += ss[i];\n if (c.size() > 0) {\n cur += c.back();\n if (x > y && cur == \"ab\") {\n c.pop_back(); ans += y; continue;\n } else if (x <= y && cur == \"ba\") {\n c.pop_back(); ans += x; continue;\n }\n }\n c.push_back(ss[i]);\n }\n return ans;\n }\n};",
"memory": "28890"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // int helper(string s, int x, int y, int i, int n, vector<char> &v){\n // if(i>=n) return 0;\n // int temp1=0, temp2=0, temp3=0, temp4=0, temp5=0;\n \n // if(v.size()>0 && s[i]=='b' && v.back()=='a'){\n // char c = v.back();\n // v.pop_back();\n // temp1 = x+helper(s, x, y, i+1, n, v);\n // v.push_back(c);\n // v.push_back(s[i]);\n // temp2 = helper(s, x, y, i+1, n, v);\n // }\n // else if(v.size()>0 && s[i]=='a' && v.back()=='b'){\n // char c = v.back();\n // v.pop_back();\n // temp3 = y+helper(s, x, y, i+1, n, v);\n // v.push_back(c);\n // v.push_back(s[i]);\n // temp4 = helper(s, x, y, i+1, n, v);\n // }\n // else{\n // v.push_back(s[i]);\n // temp5 = helper(s, x, y, i+1, n, v);\n // }\n // return max(temp5, max(max(temp1, temp2), max(temp3, temp4)));\n // }\n int helper1(string s, char a, char b, int p, vector<char> &v){\n int n = s.size(), ans=0;\n v.push_back(s[0]);\n for(int i=1; i<n; i++){\n if(v.empty()==false && s[i]==b && v.back()==a){\n v.pop_back();\n ans += p;\n }\n else v.push_back(s[i]);\n }\n return ans;\n }\n int helper2(vector<char> &v, int a, int b, int m){\n vector<char> v2;\n v2.push_back(v[0]);\n int n = v.size(), ans=0;\n for(int i=1; i<n; i++){\n if(v2.empty()==false && v[i]==b && v2.back()==a){\n v2.pop_back();\n ans += m;\n }\n else v2.push_back(v[i]);\n }\n return ans;\n }\n int maximumGain(string s, int x, int y)\n {\n char a='a', b='b';\n if(y>x){\n a = 'b';\n b = 'a';\n }\n vector<char> v;\n int p = max(x,y), m=min(x,y);\n int ans1 = helper1(s, a, b, p, v);\n\n int n = v.size();\n for(int i=0; i<n; i++) cout<<v[i]<<\" \";\n \n int ans2 = helper2(v, b, a, m);\n return ans1+ans2;\n }\n // {\n // vector<char> v;\n // v.push_back(s[0]);\n // return helper(s, x, y, 1, s.size(), v);\n\n // }\n};",
"memory": "28890"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int score = 0;\n vector<char>v(s.begin(), s.end());\n\n function<void(string,int)> f = [&] (string p, int s) -> void {\n if(v.empty())return;\n vector<char>temp;\n temp.push_back(v[0]);\n for(int i = 1; i < v.size(); i++) {\n if(!temp.empty() && temp.back() == p[0] && v[i] == p[1]) {\n temp.pop_back();\n score += s;\n } else {\n temp.push_back(v[i]);\n }\n }\n v = temp;\n };\n\n if(x > y) {\n f(\"ab\", x);\n f(\"ba\", y);\n } else {\n f(\"ba\", y);\n f(\"ab\", x);\n }\n\n return score;\n }\n};",
"memory": "29309"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n if(s.length()<2) return 0;\n int score=0;\n char first,second;\n int add1,add2;\n if(x>y) {\n add1=x;\n add2=y;\n second='b';\n first='a';\n }\n else {\n add1=y;\n add2=x;\n second='a';\n first='b';\n }\n int i=1;\n stack<char>st;\n st.push(s[0]);\n while(i<s.length()){\n if(!st.empty() && st.top()==first && s[i]==second){\n score+=add1;\n st.pop();\n }\n else st.push(s[i]);\n i++;\n }\n stack<int>temp;\n while(!st.empty()){\n if(!temp.empty() && st.top()==second && temp.top()==first){\n score+=add2;\n temp.pop();\n }\n else temp.push(st.top());\n st.pop();\n }\n return score;\n }\n};",
"memory": "29309"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int helper(string s,string s1,int max,int min){\n cout<<s1<<\" \";\n int n=s.length();\n stack<char>st;\n int ans=0;\n st.push(s[0]);\n int i=1;\n while(i<n){\n if( !st.empty() && s[i]==s1[1] && st.top()==s1[0] ){\n st.pop();\n ans+=max;\n }\n else\n st.push(s[i]);\n i++;\n \n }\n string temp;\n while(!st.empty()){\n char c=st.top();\n st.pop();\n temp.push_back(c);\n }\n reverse(temp.begin(),temp.end());\n\n int j=1;\n st.push(temp[0]);\n while(j<temp.length()){\n \n if( !st.empty() && temp[j]==s1[0] && st.top()==s1[1] ){\n st.pop();\n cout<<\"hello\";\n ans+=min;\n }\n else\n st.push(temp[j]);\n j++;\n }\n return ans;\n }\n int maximumGain(string s, int x, int y) {\n int ans=0;\n\n if(x > y)\n ans= helper(s,\"ab\",x,y);\n \n else{\n ans= helper(s,\"ba\",y,x);\n }\n\n return ans;\n }\n};\n\n\n",
"memory": "29728"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int n=s.size() ;\n if(n==1) return 0 ;\n else if(n==2){\n if(s==\"ab\") return x ;\n else if(s==\"ba\") return y ;\n else return 0 ;\n }\n int maxi=0 ;\n if(x>=y){\n int cnt=0;\n stack<int>st ;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'){\n st.push(s[i]) ; \n }else if(s[i]=='b'){\n if(!st.empty()&&st.top()=='a'){\n maxi+=x ;\n st.pop() ;\n }\n else{\n st.push(s[i]) ;\n }\n }\n else{\n st.push(s[i]) ;\n }\n }\n while(!st.empty()){\n char t=st.top() ;\n st.pop() ;\n if(t=='a'){\n cnt++;\n }\n else if(t=='b'){\n cnt-- ;\n if(cnt>=0) maxi+=y ;\n else cnt=0 ;\n }\n else cnt=0 ;\n }\n }\n else{\n int cnt=0 ;\n stack<int>st ;\n for(int i=0;i<s.size();i++){\n if(s[i]=='b'){\n st.push(s[i]) ;\n }else if(s[i]=='a'){\n if(!st.empty()&&st.top()=='b'){\n maxi+=y ;\n st.pop() ;\n }\n else st.push(s[i]) ;\n }\n else{\n st.push(s[i]) ;\n }\n }\n while(!st.empty()){\n char t=st.top() ;\n st.pop() ;\n if(t=='b'){\n cnt++;\n }\n else if(t=='a'){\n cnt-- ;\n if(cnt>=0) maxi+=x ;\n else cnt=0 ;\n }\n else cnt=0 ;\n }\n }\n return maxi ;\n }\n};",
"memory": "29728"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int points = 0;\n\n // Determine which pattern gives more points\n char first = 'a', second = 'b';\n if (x < y) {\n swap(first, second);\n swap(x, y);\n }\n\n points += removeAndCount(s, first, second, x);\n points += removeAndCount(s, second, first, y);\n\n return points;\n }\n\nprivate:\n int removeAndCount(string& s, char first, char second, int pointsForPair) {\n int points = 0;\n vector<char> stack;\n\n for (char c : s) {\n if (!stack.empty() && stack.back() == first && c == second) {\n stack.pop_back(); // remove the valid pair\n points += pointsForPair;\n } else {\n stack.push_back(c);\n }\n }\n\n s = string(stack.begin(), stack.end()); // Rebuild the string with remaining characters\n return points;\n }\n};\n",
"memory": "30146"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int totalScore = 0;\n string highPriorityPair = x > y ? \"ab\" : \"ba\";\n string lowPriorityPair = highPriorityPair == \"ab\" ? \"ba\" : \"ab\";\n\n // First pass: remove high priority pair\n string stringAfterFirstPass = removeSubstring(s, highPriorityPair);\n int removedPairsCount =\n (s.length() - stringAfterFirstPass.length()) / 2;\n\n // Calculate score from first pass\n totalScore += removedPairsCount * max(x, y);\n\n // Second pass: remove low priority pair\n string stringAfterSecondPass =\n removeSubstring(stringAfterFirstPass, lowPriorityPair);\n removedPairsCount =\n (stringAfterFirstPass.length() - stringAfterSecondPass.length()) /\n 2;\n\n // Calculate score from second pass\n totalScore += removedPairsCount * min(x, y);\n\n return totalScore;\n }\n\nprivate:\n string removeSubstring(const string& input, const string& targetPair) {\n stack<char> charStack;\n\n // Iterate through each character in the input string\n for (char currentChar : input) {\n // Check if current character forms the target pair with the top of\n // the stack\n if (currentChar == targetPair[1] && !charStack.empty() &&\n charStack.top() == targetPair[0]) {\n charStack\n .pop(); // Remove the matching character from the stack\n } else {\n charStack.push(currentChar);\n }\n }\n\n // Reconstruct the remaining string after removing target pairs\n string remainingChars;\n while (!charStack.empty()) {\n remainingChars += charStack.top();\n charStack.pop();\n }\n reverse(remainingChars.begin(), remainingChars.end());\n return remainingChars;\n }\n};",
"memory": "30146"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int totalScore = 0;\n string highPriorityPair = x > y ? \"ab\" : \"ba\";\n string lowPriorityPair = highPriorityPair == \"ab\" ? \"ba\" : \"ab\";\n\n // First pass: remove high priority pair\n string stringAfterFirstPass = removeSubstring(s, highPriorityPair);\n int removedPairsCount =\n (s.length() - stringAfterFirstPass.length()) / 2;\n\n // Calculate score from first pass\n totalScore += removedPairsCount * max(x, y);\n\n // Second pass: remove low priority pair\n string stringAfterSecondPass =\n removeSubstring(stringAfterFirstPass, lowPriorityPair);\n removedPairsCount =\n (stringAfterFirstPass.length() - stringAfterSecondPass.length()) /\n 2;\n\n // Calculate score from second pass\n totalScore += removedPairsCount * min(x, y);\n\n return totalScore;\n }\n\nprivate:\n string removeSubstring(const string& input, const string& targetPair) {\n stack<char> charStack;\n\n // Iterate through each character in the input string\n for (char currentChar : input) {\n // Check if current character forms the target pair with the top of\n // the stack\n if (currentChar == targetPair[1] && !charStack.empty() &&\n charStack.top() == targetPair[0]) {\n charStack\n .pop(); // Remove the matching character from the stack\n } else {\n charStack.push(currentChar);\n }\n }\n\n // Reconstruct the remaining string after removing target pairs\n string remainingChars;\n while (!charStack.empty()) {\n remainingChars += charStack.top();\n charStack.pop();\n }\n reverse(remainingChars.begin(), remainingChars.end());\n return remainingChars;\n }\n};",
"memory": "30565"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int points=0;\n string addval(char first , char second , int p , string s){\n string ns;\n int poi=0;\n for(char c : s){\n if(!ns.empty() && ns.back()==first && c==second){\n poi+=p;\n ns.pop_back();\n }else{\n ns.push_back(c);\n }\n }\n points+=poi;\n return ns;\n }\n int maximumGain(string s, int x, int y) {\n if(y > x){\n s = addval('b','a',y,s);\n s = addval('a','b',x,s);\n }\n if(x >= y){\n s = addval('a','b',x,s);\n s = addval('b','a',y,s);\n }\n return points;\n }\n};",
"memory": "30565"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxG(string s,int x,int y,string xs,string ys){\n if(y>x){\n return maxG(s,y,x,ys,xs);\n }\n stack<char>st;\n int ans=0;\n for(int i=0;i<s.length();i++){\n if(!st.empty() && s[i]==xs[1] && st.top()==xs[0]){\n ans+=x;\n st.pop();\n }\n else{\n st.push(s[i]);\n }\n }\n string rev=\"\";\n while(!st.empty()){\n rev+=st.top();\n st.pop();\n }\n reverse(rev.begin(),rev.end());\n for(int i=0;i<rev.length();i++){\n if(!st.empty() && rev[i]==ys[1] && st.top()==ys[0]){\n ans+=y;\n st.pop();\n }\n else{\n st.push(rev[i]);\n }\n }\n return ans;\n }\n int maximumGain(string s, int x, int y) {\n return maxG(s,x,y,\"ab\",\"ba\");\n }\n};",
"memory": "30984"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n string d;\n if(x>y) {\n d=\"ab\";\n }\n else {\n d=\"ba\";\n }\n vector<char>st;\n\n for(int i=0;i<s.length();i++) {\n if(st.size()==0) {\n st.push_back(s[i]);\n continue;\n }\n char f = st[st.size()-1];\n\n //string c = f+s[i];\n if(f==d[0] && s[i]==d[1]) {\n st.pop_back();\n\n }\n else {\n st.push_back(s[i]);\n }\n }\n string v =\"\";\n for(int i=0;i<st.size();i++) {\n v+=st[i];\n }\n int ans=0;\n if(d==\"ab\") {\n ans+=((s.length()-st.size())/2)*x;\n d=\"ba\";\n }\n else {\n ans+=((s.length()-st.size())/2)*y;\n d=\"ab\";\n }\n //cout<<ans<<endl;\n vector<char>h;\n for(int i=0;i<v.length();i++) {\n if(h.size()==0) {\n h.push_back(v[i]);\n continue;\n }\n char t = h[h.size()-1];\n //string g = t+v[i];\n if(t==d[0] && v[i]==d[1]) {\n h.pop_back();\n }\n else {\n h.push_back(v[i]);\n }\n\n }\n if(d==\"ab\") {\n ans+=((st.size()-h.size())/2)*x;\n \n }\n else {\n ans+=((st.size()-h.size())/2)*y;\n \n }\n //cout<<ans<<endl;\n return ans;\n\n }\n};\n// class Solution {\n// public:\n// int maximumGain(string s, int x, int y) {\n// int n = s.length();\n// int score = 0;\n\n// string maxStr = (x > y) ? \"ab\" : \"ba\";\n// string minStr = (x < y) ? \"ab\" : \"ba\";\n\n// //First Pass\n// string temp_first = removeSubstring(s, maxStr);\n// int removedPairsCount = (n - temp_first.length()) / 2;\n// score += removedPairsCount * max(x, y);\n// cout<<score<<endl;\n\n\n// //Second Pass\n// string temp_second = removeSubstring(temp_first, minStr);\n// removedPairsCount = (temp_first.length() - temp_second.length()) / 2;\n// score += removedPairsCount * min(x, y);\n// cout<<score<<endl;\n\n// return score;\n// }\n\n// private:\n// string removeSubstring(string& s, string& matchStr) {\n// stack<char> st;\n\n// for (char &ch : s) {\n// if (ch == matchStr[1] && !st.empty() && st.top() == matchStr[0]) {\n// st.pop();\n// } else {\n// st.push(ch);\n// }\n// }\n\n// string remainStr;\n// while (!st.empty()) {\n// remainStr.push_back(st.top());\n// st.pop();\n// }\n// reverse(remainStr.begin(), remainStr.end());\n// return remainStr;\n// }\n// };",
"memory": "31403"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(stack<char> &s){\n string ans;\n while(!s.empty()){\n ans.push_back(s.top());\n s.pop();\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n int solve(string str,char ch1,char ch2,int c,stack<char> &s){\n int n=str.size();\n int cost=0;\n for(int i=0;i<n;i++){\n char ch=str[i];\n if(s.empty()){\n s.push(ch);\n }\n else if(s.top()==ch1&&ch==ch2){\n cost=cost+c;\n s.pop();\n }\n else{\n s.push(ch);\n }\n }\n return cost;\n }\n int maximumGain(string str, int x, int y) {\n int n=str.size();\n stack<char> s;\n int cost=0;\n if(y>x){\n cost+=solve(str,'b','a',y,s);\n str=convert(s);\n cost+=solve(str,'a','b',x,s);\n }\n else{\n cost+=solve(str,'a','b',x,s);\n str=convert(s);\n cost+=solve(str,'b','a',y,s);\n }\n return cost;\n }\n};",
"memory": "31403"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint maximumGain(string x,int first,int second)\n{\n if(first>second)\n return solve (x,first,second,\"ab\",\"ba\")+solve (x,second,first,\"ba\",\"ab\");\n else \n return solve (x,second,first,\"ba\",\"ab\")+solve (x,first,second,\"ab\",\"ba\");\n}\n int solve (string &s, int &x, int &y,string first,string second) {\n int ans=0;\n stack<char>st;\n for(int i=0;i<s.length();i++)\n {\n if(st.empty())\n {\n st.push(s[i]);\n }\n else \n {\n // cout<<st.top()<<endl;\n string s1=\"\";\n s1=s1+st.top();\n s1=s1+s[i];\n // cout<<s1<<endl;\n if(s1==first)\n {\n ans=ans+x;\n st.pop();\n }\n \n else \n st.push(s[i]);\n }\n }\n s=stack_to_string(st);\n return ans;\n }\n string stack_to_string(stack<char>& st) {\n string str = \"\";\n while (!st.empty()) {\n str += st.top();\n st.pop();\n }\n reverse(str.begin(), str.end());\n return str;\n }\n};",
"memory": "31821"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int maximumGainPass1(string s, string subs,int gain, stack<char>& stk) {\n char first = subs[0];\n char second = subs[1];\n int totalGain = 0;\n for (auto c : s) {\n if (c == second) {\n if (!stk.empty() && stk.top() == first) {\n stk.pop();\n totalGain+=gain;\n }else {\n stk.push(c);\n }\n }\n else {\n stk.push(c);\n }\n }\n return totalGain;\n }\n int maximumGainPass2(string subs, int gain, stack<char>& stk) {\n string s;\n \n while (!stk.empty()) {\n s += stk.top();\n stk.pop();\n }\n reverse(s.begin(), s.end());\n int totalGain = maximumGainPass1(s, subs, gain, stk);\n return totalGain;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n stack<char> stk;\n int totalGain = 0;\n if (x > y) {\n totalGain = maximumGainPass1(s, \"ab\", x, stk);\n totalGain += maximumGainPass2(\"ba\", y, stk);\n }\n else {\n totalGain = maximumGainPass1(s, \"ba\", y, stk);\n totalGain += maximumGainPass2(\"ab\", x, stk);\n }\n return totalGain ; \n }\n};",
"memory": "31821"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int getScore(int len, int x, int y){\n return max(x + (len-2)/2 * y, len/2 * x);\n }\n\n int resolveString(string s, char c1, char c2, int x, int y){\n int n = s.size();\n int ans = 0;\n\n string res;\n for(int i = 0;i<n;i++){\n if(s[i] == c2 && res.size()){\n if(res[res.size()-1] == c1){\n ans+=x;\n res.pop_back();\n } else {\n res.push_back(s[i]);\n }\n } else {\n res.push_back(s[i]);\n }\n }\n\n s = res;\n res.clear();\n\n for(int i = 0;i<s.size();i++){\n if(s[i] == c1 && res.size()){\n if(res[res.size()-1] == c2){\n ans+=y;\n res.pop_back();\n } else {\n res.push_back(s[i]);\n }\n } else {\n res.push_back(s[i]);\n }\n }\n\n return ans;\n }\n\n int maximumGain(string s, int x, int y) {\n int n = s.size();\n int ans = 0;\n ans = max(resolveString(s, 'a', 'b', x, y), resolveString(s, 'b', 'a', y, x));\n return ans;\n }\n};",
"memory": "32240"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int getScore(int len, int x, int y){\n return max(x + (len-2)/2 * y, len/2 * x);\n }\n\n int resolveString(string s, char c1, char c2, int x, int y){\n int n = s.size();\n int ans = 0;\n\n string res;\n for(int i = 0;i<n;i++){\n if(s[i] == c2 && res.size()){\n if(res[res.size()-1] == c1){\n ans+=x;\n res.pop_back();\n } else {\n res.push_back(s[i]);\n }\n } else {\n res.push_back(s[i]);\n }\n }\n\n cout << res << endl;\n s = res;\n res.clear();\n\n for(int i = 0;i<s.size();i++){\n if(s[i] == c1 && res.size()){\n if(res[res.size()-1] == c2){\n ans+=y;\n res.pop_back();\n } else {\n res.push_back(s[i]);\n }\n } else {\n res.push_back(s[i]);\n }\n }\n\n return ans;\n }\n\n int maximumGain(string s, int x, int y) {\n int n = s.size();\n int ans = 0;\n ans = max(resolveString(s, 'a', 'b', x, y), resolveString(s, 'b', 'a', y, x));\n return ans;\n }\n};",
"memory": "32659"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n long long ans=0;\n\n string absolve(string s,int val){\n string st=\"\";\n for(auto x:s){\n if(x=='b'){\n if(st.size() && st.back()=='a'){\n ans+=val;\n st.pop_back();\n }\n else st+=x;\n }\n else st+=x;\n }\n return st;\n }\n\n string basolve(string s,int val){\n string st=\"\";\n for(auto x:s){\n if(x=='a'){\n if(st.size() && st.back()=='b'){\n ans+=val;\n st.pop_back();\n }\n else st+=x;\n }\n else st+=x;\n }\n return st;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n string res=s;\n if(x>y){\n res=absolve(res,x);\n basolve(res,y);\n }\n else{\n res=basolve(res,y);\n absolve(res,x);\n }\n return ans;\n }\n};",
"memory": "32659"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n return solve(s, x, y);\n }\n\nprivate:\n int solve(string s, int x, int y) {\n string maxstr = (x > y) ? \"ab\" : \"ba\";\n string minstr = (x > y) ? \"ba\" : \"ab\";\n int score = 0;\n string temp_one = remove(s, maxstr, score, max(x, y));\n string temp_two = remove(temp_one, minstr, score, min(x, y));\n return score;\n }\n\n string remove(string s, const string& str, int& score, int points) {\n string result;\n int i = 0;\n while (i < s.size()) {\n if (result.size() >= 1 && result.back() == str[0] && s[i] == str[1]) {\n result.pop_back();\n score += points;\n } else {\n result.push_back(s[i]);\n }\n i++;\n }\n return result;\n }\n};\n",
"memory": "33078"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int scoree(string& pair, int score,string& s){\n stack<char> st;\n\n int res=0;\n for(auto x: s){\n if(x==pair[1] &&!st.empty() && st.top()==pair[0] ){\n \n res+=score;\n st.pop();\n \n \n\n }\n else{st.push(x);}\n \n \n }\n string news=\"\";\n while(!st.empty()){\n news+=st.top();\n st.pop();\n }\n s=news;\n reverse(s.begin(),s.end());\n return res;\n }\n int maximumGain(string s, int x1, int y1) {\n //2 phase\n\n string pair=(x1>y1 )? \"ab\": \"ba\";\n string ns=s;\n cout<<ns<<\"\\n\";\n int first=scoree(pair,max(x1,y1),ns);\n reverse(pair.begin(),pair.end());\n cout<<ns<<\"\\n\";\n int sec=scoree(pair,min(x1,y1),ns);\n return first+sec;\n\n }\n};",
"memory": "33078"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n std::pair<int, std::string> remove_substring(std::string s, char c1, char c2) {\n std::string remaining;\n int res = 0;\n\n for (auto ch : s) {\n if (ch == c2) {\n if (!remaining.empty() && remaining.back() == c1) {\n remaining.pop_back();\n res++;\n } else {\n remaining.push_back(ch);\n }\n } else {\n remaining.push_back(ch);\n }\n }\n \n return {res, remaining};\n }\n\n int maximumGain(string s, int x, int y) {\n int res = 0;\n std::vector<std::pair<int, int>> order = {\n {'a', 'b'},\n {'b', 'a'},\n };\n\n if (x < y) {\n std::reverse(order.begin(), order.end());\n }\n\n auto [points, rem] = remove_substring(s, order[0].first, order[0].second);\n auto[points2, _] = remove_substring(rem, order[1].first, order[1].second);\n\n return std::max(x, y) * points + std::min(x, y) * points2;\n\n }\n};",
"memory": "33496"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n std::pair<int, std::string> remove_substring(std::string s, char c1, char c2) {\n std::string remaining;\n int res = 0;\n\n for (auto ch : s) {\n if (ch == c2) {\n if (!remaining.empty() && remaining.back() == c1) {\n remaining.pop_back();\n res++;\n } else {\n remaining.push_back(ch);\n }\n } else {\n remaining.push_back(ch);\n }\n }\n \n return {res, remaining};\n }\n\n int maximumGain(string s, int x, int y) {\n int res = 0;\n std::vector<std::pair<int, int>> order = {\n {'a', 'b'},\n {'b', 'a'},\n };\n\n if (x < y) {\n std::reverse(order.begin(), order.end());\n }\n\n auto [points, rem] = remove_substring(s, order[0].first, order[0].second);\n auto[points2, _] = remove_substring(rem, order[1].first, order[1].second);\n\n return std::max(x, y) * points + std::min(x, y) * points2;\n\n }\n};",
"memory": "33496"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string remove(string s,string t)\n {\n stack<char> st;\n for(int i=0;i<s.length();i++)\n {\n if(st.size()>0 && s[i]==t[1] && st.top()==t[0]) st.pop();\n else st.push(s[i]);\n }\n\n string new_string;\n while(st.size()>0)\n {\n new_string.push_back(st.top());\n st.pop();\n }\n reverse(new_string.begin(),new_string.end());\n return new_string;\n }\n int maximumGain(string s, int x, int y) {\n string maxi = \"\";\n string mini = \"\";\n if(x>y) {\n maxi = \"ab\";\n mini = \"ba\";\n }\n else{\n maxi = \"ba\";\n mini = \"ab\";\n }\n\n int n = s.length();\n string first = remove(s,maxi);\n int new_length = n - first.length();\n int no_of_pairs = new_length/2;\n int result1 = no_of_pairs * max(x,y);\n\n string second = remove(first,mini);\n int new_length1 = first.length() - second.length();\n int no_of_pairs1 = new_length1/2;\n int result2 = no_of_pairs1 * min(x,y);\n\n return result1+result2;\n }\n};",
"memory": "33915"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.