id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "// #include <ext/pb_ds/assoc_container.hpp> // Common file\n// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update\n// #include <ext/pb_ds/detail/standard_policies.hpp>\n// using namespace __gnu_pbds;\n\n// typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n\n// class Solution {\n// public:\n// const int mod = 1e9+7;\n// int createSortedArray(vector<int>& nums) {\n// ordered_set st1, st2;\n// int ans=0;\n\n// for(int i=0;i<nums.size();i++){\n// int x = nums[i];\n// int sm = st1.order_of_key(x);\n// int lx = st2.order_of_key(-1*x);\n// // cout<<sm<<\" \"<<lx<<endl;\n// st1.insert(x);\n// st2.insert(-1*x);\n// ans= (ans+min(sm,lx))%mod;\n// }\n// return ans;\n// }\n// };\nclass Solution {\npublic:\n const int mod = 1e9+7;\n vector<int> lesser, greater;\n void merge(int left,int mid, int right, vector<pair<int,int>> &v){\n\n for(int i = mid + 1, j = left, k = left; i <= right; i++) {\n while(j <= mid and v[j].first < v[i].first) j++;\n while(k <= mid and v[k].first <= v[i].first) k++;\n lesser[v[i].second] += j - left;\n greater[v[i].second] += mid - k + 1;\n }\n\n vector<pair<int,int>> temp(right - left + 1);\n int i=left,j=mid+1,k=0;\n\n while(i<=mid && j<=right){\n if(v[i].first<=v[j].first){\n temp[k++]=v[i++];\n }\n else{\n temp[k++]=v[j++];\n }\n }\n while(i<=mid){\n temp[k++]=v[i++];\n }\n while(j<=right){\n temp[k++]=v[j++];\n }\n for(int i=left;i<=right;i++){\n v[i]=temp[i-left];\n }\n // for(auto x: temp){cout<<x.first<<\" \";}\n // cout<<endl;\n }\n void mergeSort(int l, int r,vector<pair<int,int>> &v){\n if(l>=r)return;\n int mid = (l+r)/2;\n mergeSort(l, mid,v);\n mergeSort(mid+1, r, v);\n\n merge(l, mid, r, v);\n }\n \n int createSortedArray(vector<int>& nums) {\n int n= nums.size();\n lesser.resize(n);\n greater.resize(n);\n\n vector<pair<int,int>> v;\n for(int i=0;i<n;i++){\n v.push_back({nums[i], i});\n } \n\n mergeSort(0, n-1, v);\n\n int ans=0;\n for(int i=0;i<n;i++){\n ans= (ans+min(lesser[i], greater[i]))%mod;\n }\n return ans;\n }\n};",
"memory": "351831"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n vector<pair<int,int>> index; // vector of pairs (value, index)\n vector<int> lesser, greater; // lesser[i] = number of values in the left of i that are less than i\n // greater[i] = number of values in the right of i that are greater than i\n \n void merge(int left, int mid, int right) {\n // calculate lesser and greater for each element in the right subarray\n for(int i = mid + 1, j = left, k = left; i <= right; i++) {\n while(j <= mid and index[j].first < index[i].first) j++;\n while(k <= mid and index[k].first <= index[i].first) k++;\n lesser[index[i].second] += j - left;\n greater[index[i].second] += mid - k + 1;\n }\n\n // merge the two sorted subarrays\n vector<pair<int,int>> temp(right - left + 1);\n int p = left, q = mid + 1, r = 0;\n while (p <= mid and q <= right) \n if(index[p].first <= index[q].first)\n temp[r++] = index[p++];\n else\n temp[r++] = index[q++];\n while (p <= mid) \n temp[r++] = index[p++];\n while (q <= right)\n temp[r++] = index[q++];\n \n // copy the merged subarray back to the original array\n move(temp.begin(), temp.end(), index.begin() + left);\n }\n\n void mergeSort(int left, int right) {\n if(left >= right)\n return;\n int mid = left + (right - left) / 2;\n mergeSort(left, mid);\n mergeSort(mid + 1, right);\n merge(left, mid, right);\n }\n\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n index.resize(n);\n lesser.resize(n);\n greater.resize(n);\n\n // initialize index vector with pairs (value, index)\n for (int i = 0; i < n; i++)\n index[i] = {instructions[i], i};\n \n // sort the index vector using merge sort and calculate lesser and greater for each element\n mergeSort(0, n - 1);\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n // calculate the minimum of the two values and add it to the answer\n ans += min(lesser[i], greater[i]);\n ans %= mod;\n }\n return ans;\n }\n};",
"memory": "356143"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n vector<pair<int,int>> index; // vector of pairs (value, index)\n vector<int> lesser, greater; // lesser[i] = number of values in the left of i that are less than i\n // greater[i] = number of values in the right of i that are greater than i\n \n void merge(int left, int mid, int right) {\n // calculate lesser and greater for each element in the right subarray\n for(int i = mid + 1, j = left, k = left; i <= right; i++) {\n while(j <= mid and index[j].first < index[i].first) j++;\n while(k <= mid and index[k].first <= index[i].first) k++;\n lesser[index[i].second] += j - left;\n greater[index[i].second] += mid - k + 1;\n }\n\n // merge the two sorted subarrays\n vector<pair<int,int>> temp(right - left + 1);\n int p = left, q = mid + 1, r = 0;\n while (p <= mid and q <= right) \n if(index[p].first <= index[q].first)\n temp[r++] = index[p++];\n else\n temp[r++] = index[q++];\n while (p <= mid) \n temp[r++] = index[p++];\n while (q <= right)\n temp[r++] = index[q++];\n \n // copy the merged subarray back to the original array\n move(temp.begin(), temp.end(), index.begin() + left);\n }\n\n void mergeSort(int left, int right) {\n if(left >= right)\n return;\n int mid = left + (right - left) / 2;\n mergeSort(left, mid);\n mergeSort(mid + 1, right);\n merge(left, mid, right);\n }\n\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n index.resize(n);\n lesser.resize(n);\n greater.resize(n);\n\n // initialize index vector with pairs (value, index)\n for (int i = 0; i < n; i++)\n index[i] = {instructions[i], i};\n \n // sort the index vector using merge sort and calculate lesser and greater for each element\n mergeSort(0, n - 1);\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n // calculate the minimum of the two values and add it to the answer\n ans += min(lesser[i], greater[i]);\n ans %= mod;\n }\n return ans;\n }\n};",
"memory": "360456"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n vector<pair<int,int>> index; \n vector<int> lesser, greater; \n void merge(int left, int mid, int right) {\n for(int i = mid + 1, j = left, k = left; i <= right; i++) {\n while(j <= mid and index[j].first < index[i].first) j++;\n while(k <= mid and index[k].first <= index[i].first) k++;\n lesser[index[i].second] += j - left;\n greater[index[i].second] += mid - k + 1;\n }\n vector<pair<int,int>> temp(right - left + 1);\n int p = left, q = mid + 1, r = 0;\n while (p <= mid and q <= right) \n if(index[p].first <= index[q].first)\n temp[r++] = index[p++];\n else\n temp[r++] = index[q++];\n while (p <= mid) \n temp[r++] = index[p++];\n while (q <= right)\n temp[r++] = index[q++];\n move(temp.begin(), temp.end(), index.begin() + left);\n }\n\n void mergeSort(int left, int right) {\n if(left >= right)\n return;\n int mid = left + (right - left) / 2;\n mergeSort(left, mid);\n mergeSort(mid + 1, right);\n merge(left, mid, right);\n }\n\n int createSortedArray(vector<int>& instructions) {\n int n = instructions.size();\n index.resize(n);\n lesser.resize(n);\n greater.resize(n);\n for (int i = 0; i < n; i++)\n index[i] = {instructions[i], i};\n mergeSort(0, n - 1);\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n ans += min(lesser[i], greater[i]);\n ans %= mod;\n }\n return ans;\n }\n};",
"memory": "364768"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class Solution {\npublic:\n void merge(vector<pair<int,int>>&a,vector<int>&small,vector<int>&large,int i,int mid,int j){\n vector<pair<int,int>>temp(j-i+1);\n int ptr=i,i1=i,j1=mid+1;\n for(int x=mid+1;x<=j;x++){\n while(ptr<=mid and a[ptr].first<a[x].first)ptr++;\n small[a[x].second]+=ptr-i;\n }\n ptr=i;\n for(int x=mid+1;x<=j;x++){\n while(ptr<=mid and a[ptr].first<=a[x].first)ptr++;\n large[a[x].second]+=mid-ptr+1;\n }\n ptr=0;\n while(i1<=mid and j1<=j){\n if(a[i1]<=a[j1]){\n temp[ptr++]=a[i1++];\n }else{\n temp[ptr++]=a[j1++];\n }\n }\n while(i1<=mid){\n temp[ptr++]=a[i1++];\n }\n while(j1<=j){\n temp[ptr++]=a[j1++];\n }\n ptr=0;\n while(ptr<j-i+1){\n a[i+ptr]=temp[ptr];\n ptr++;\n }\n }\n void solve(vector<pair<int,int>>&a,vector<int>&small,vector<int>&large,int i,int j){\n if(i>=j)return ;\n int mid=i+(j-i)/2;\n solve(a,small,large,i,mid);solve(a,small,large,mid+1,j);\n merge(a,small,large,i,mid,j);\n }\n int createSortedArray(vector<int>& in,int MOD=1e9+7) {\n int n=in.size(),ans=0;\n vector<int>small(n),large(n);\n vector<pair<int,int>>v;\n for(auto i=0;i<n;i++)v.push_back({in[i],i});\n solve(v,small,large,0,n-1);\n for(int i=0;i<n;i++){\n ans=(ans+min(small[i],large[i]))%MOD;\n }\n return ans;\n }\n};",
"memory": "369081"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "const int N=1e5+10;\nint smaller[N],larger[N];\nconst int M=1e9+7;\nclass Solution {\npublic:\n void merge(vector<pair<int,int>>&arr,int l,int m,int r){\n for(int i = m + 1, j = l, k = l; i <= r; i++) {\n while(j <= m and arr[j].first < arr[i].first) j++;\n while(k <= m and arr[k].first <= arr[i].first) k++;\n smaller[arr[i].second] += j - l;\n larger[arr[i].second] += m - k + 1;\n }\n int n1=m-l+1,n2=r-m;\n vector<pair<int,int>>left(n1),right(n2);\n for(int i=0;i<n1;i++){\n left[i]=arr[l+i];\n }\n for(int i=0;i<n2;i++){\n right[i]=arr[m+1+i];\n }\n int i=0,j=0,k=l;\n while(i<n1 and j<n2){\n if(left[i].first<=right[j].first) arr[k++]=left[i++];\n else arr[k++]=right[j++];\n }\n while(i<n1) arr[k++]=left[i++];\n while(j<n2) arr[k++]=right[j++];\n }\n void mergeSort(vector<pair<int,int>>&arr,int l,int r){\n if(l>=r) return;\n int m=l+(r-l)/2;\n mergeSort(arr,l,m);\n mergeSort(arr,m+1,r);\n merge(arr,l,m,r);\n }\n int createSortedArray(vector<int>& instructions) {\n memset(smaller,0,sizeof(smaller));\n memset(larger,0,sizeof(larger));\n int n=instructions.size();\n vector<pair<int,int>>arr(n);\n for(int i=0;i<n;i++){\n arr[i]={instructions[i],i};\n }\n mergeSort(arr,0,n-1);\n int ans=0;\n for(int i=0;i<n;i++){\n ans=(ans+(min(smaller[i],larger[i])))%M;\n }\n return ans;\n }\n};",
"memory": "373393"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n #define ll long long\n void merge(vector<pair<ll,ll>> &arr, vector<ll> &less, vector<ll> &big, ll l, ll r) {\n ll i = l;\n ll one = 1;\n ll mid = l + (r-l)/2;\n ll j = mid + one;\n \n vector<pair<ll,ll>> temp((r-l+one));\n ll ind = 0;\n while(i <= mid && j <= r) {\n if(arr[i].first > arr[j].first) {\n temp[ind] = arr[j];\n big[arr[j].second] += (mid - i + one);\n less[arr[j].second] += (i - l);\n j++; ind++;\n }\n else if(arr[i].first <= arr[j].first) {\n temp[ind] = arr[i];\n ind++;\n i++;\n }\n }\n while(j <= r) {\n temp[ind] = arr[j];\n less[arr[j].second] += (mid - l + one);\n j++; ind++;\n }\n while(i <= mid) {\n temp[ind] = arr[i];\n ind++;\n i++;\n }\n for (ll i = 0; i < (r-l+one); i++) {\n arr[l+i] = temp[i];\n }\n }\n void mergeSort(vector<pair<ll,ll>> &arr, vector<ll> &less, vector<ll> &big, ll l, ll r) {\n if(l < r) {\n ll mid = l + (r -l)/2;\n mergeSort(arr, less, big, l, mid);\n mergeSort(arr, less, big, mid + 1, r);\n merge(arr,less,big,l,r);\n }\n }\n int createSortedArray(vector<int>& nums) {\n ll n = nums.size();\n vector<pair<ll,ll>> arr(n);\n vector<ll> less(n, 0ll), big(n, 0ll), equal(n, 0ll);\n unordered_map<ll, ll> cnt;\n for (ll i = 0; i < n; i++) {\n equal[i] = cnt[nums[i]];\n cnt[nums[i]]++;\n arr[i] = {nums[i], i};\n }\n ll mod = 1e9 + 7;\n mergeSort(arr,less,big,0,n-1);\n long long ans = 0;\n for (ll i = 0; i < n; i++) {\n // cout << less[i] << \" \" << big[i] << \" \" << equal[i] << endl;\n ans += min(less[i] - equal[i], big[i]);\n ans %= mod;\n // if(ans < 0) {\n // cout << \"hi \" << ans << endl;\n // }\n }\n return ans;\n }\n};",
"memory": "377706"
} |
1,772 | <p>Given an integer array <code>instructions</code>, you are asked to create a sorted array from the elements in <code>instructions</code>. You start with an empty container <code>nums</code>. For each element from <strong>left to right</strong> in <code>instructions</code>, insert it into <code>nums</code>. The <strong>cost</strong> of each insertion is the <b>minimum</b> of the following:</p>
<ul>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly less than</strong> <code>instructions[i]</code>.</li>
<li>The number of elements currently in <code>nums</code> that are <strong>strictly greater than</strong> <code>instructions[i]</code>.</li>
</ul>
<p>For example, if inserting element <code>3</code> into <code>nums = [1,2,3,5]</code>, the <strong>cost</strong> of insertion is <code>min(2, 1)</code> (elements <code>1</code> and <code>2</code> are less than <code>3</code>, element <code>5</code> is greater than <code>3</code>) and <code>nums</code> will become <code>[1,2,3,3,5]</code>.</p>
<p>Return <em>the <strong>total cost</strong> to insert all elements from </em><code>instructions</code><em> into </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,5,6,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
The total cost is 0 + 0 + 0 + 1 = 1.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,2,3,6,5,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> instructions = [1,3,3,3,2,4,2,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Begin with nums = [].
Insert 1 with cost min(0, 0) = 0, now nums = [1].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
βββββββInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
βββββββInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
βββββββInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>
</ul> | 3 | {
"code": "class Solution {\npublic:\n void merge(int left,int mid,int right,vector<pair<int,int>>& v,vector<int>&lesser,vector<int>&greater)\n {\n for(int i = mid + 1, j = left, k = left; i <= right; i++) {\n while(j <= mid and v[j].first < v[i].first) j++;\n while(k <= mid and v[k].first <= v[i].first) k++;\n lesser[v[i].second] += j - left;\n greater[v[i].second] += mid - k + 1;\n }\n int n1=mid-left+1;\n int n2=right-mid;\n vector<pair<int,int>>l(n1);\n vector<pair<int,int>>r(n2);\n for(int i=0;i<n1;i++)\n {\n l[i]=v[left+i];\n }\n for(int i=0;i<n2;i++)\n {\n r[i]=v[mid+1+i];\n }\n int l1=0;\n int r1=0;\n int k=left;\n while(l1<n1&&r1<n2)\n {\n if(l[l1].first<r[r1].first)\n {\n v[k]=l[l1];\n l1++;\n }\n else\n {\n v[k]=r[r1];\n r1++;\n }\n k++;\n }\n while(l1<n1)\n {\n v[k]=l[l1];\n l1++;\n k++;\n }\n while(r1<n2)\n {\n v[k]=r[r1];\n r1++;\n k++;\n }\n }\n int mod=1000000007;\n void mergesort(int left,int right,vector<pair<int,int>>& v,vector<int>&lesser,vector<int>&greater)\n {\n if(left>=right)\n {\n return;\n }\n int mid=(left+right)/2;\n mergesort(left,mid,v,lesser,greater);\n mergesort(mid+1,right,v,lesser,greater);\n merge(left,mid,right,v,lesser,greater);\n \n }\n int createSortedArray(vector<int>& instructions) {\n vector<pair<int,int>>v;\n for(int i=0;i<instructions.size();i++)\n {\n v.push_back({instructions[i],i});\n }\n vector<int>lesser(instructions.size(),0);\n vector<int>greater(instructions.size(),0);\n mergesort(0,instructions.size()-1,v,lesser,greater);\n int ans = 0;\n for (int i = 0; i < instructions.size(); i++) {\n // calculate the minimum of the two values and add it to the answer\n ans += min(lesser[i], greater[i]);\n ans %= mod;\n }\n return ans;\n }\n};",
"memory": "382018"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(const string_view& sv) {\n int a = 0;\n for (const char c : sv) {\n a += (c == 'a');\n }\n \n int res = min(a, static_cast<int>(sv.size()) - a);\n int d = 0;\n for (const char c : sv) {\n if (c == 'a') {\n a--;\n } else {\n d++;\n }\n const int t = d + a;\n res = min(res, t);\n }\n\n return res;\n }\n};",
"memory": "19810"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "#include <ranges>\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2,tune=native\")\n\nbool init() {\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n return true;\n}\n\nclass Solution {\npublic:\n int minimumDeletions(const std::string& s) const noexcept {\n int b_count = 0;\n int ans = 0;\n\n for (bool is_b : s | std::views::transform(\n [](const char ch) { return ch == 'b'; })) {\n ans += std::min<int>((b_count += is_b) - ans, !is_b);\n }\n\n return ans;\n }\n};",
"memory": "19810"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(const string &s) const {\n static int acount[100001];\n\n const int n = s.size();\n acount[n] = 0;\n for(int i = n - 1; i >= 0; i--) {\n acount[i] = acount[i + 1] + (s[i] == 'a');\n }\n\n int acnt = 0, bcnt = 0, res = acount[0];\n for(int i = 0; i < n; i++) {\n s[i] == 'a' ? acnt++ : bcnt++;\n res = min(res, bcnt + acount[i] - 1);\n }\n\n return res; \n }\n};",
"memory": "20230"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(const string& s) {\n stack<char> stk;\n int min_del = 0;\n for (auto&& c: s) {\n if (c == 'a' && !stk.empty() && stk.top() == 'b')\n stk.pop(), min_del++;\n else if (c == 'b')\n stk.push(c);\n }\n\n return min_del;\n }\n};",
"memory": "20230"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string &str) {\n int n = str.size();\n int cnt[n+1][2]; // pos will go from 1 to last + 1\n cnt[0][0] = 0;\n cnt[0][1] = 0;\n\n for(int i = 0; i < n; i++){\n cnt[i + 1][0] = cnt[i][0] + (str[i] == 'a');\n cnt[i + 1][1] = cnt[i][1] + (str[i] == 'b');\n }\n int ans = n;\n int totalA = cnt[n][0];\n for(int i = 0; i <= n; i++){\n int leftB = cnt[i][1];\n int rightA = totalA - cnt[i][0]; //// for aaaa this last count will become n\n ans = min(ans, leftB + rightA);\n }\n\n\n\n\n return ans;\n }\n};\n\n\n/*\n\nwe will keep count of a and b at every position\n a in the right side\n b in the left side of current position\n\nwe will take the min out of all\n\n*/",
"memory": "20650"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "const char optimize = []() {std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 'c';}();\n\nclass Solution {\npublic:\n int minimumDeletions(const std::string& s) const noexcept {\n int end = s.size() - 1, start = 0;\n while(start < s.size() && s[start] == 'a') ++start;\n while(end >= 0 && s[end] == 'b') --end;\n std::stack<char> stack; \n int count = 0;\n stack.push(s[start]);\n for(int i = start + 1; i <= end; ++i){\n if(!stack.empty() && stack.top() == 'b' && s[i] == 'a'){\n stack.pop();\n ++count;\n continue;\n }\n stack.push(s[i]);\n }\n return count;\n }\n};",
"memory": "20650"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\nstatic const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int minimumDeletions(string const& s) {\n std::vector<char> stack;\n stack.reserve(s.size());\n int count {0};\n for (char c : s) {\n if (!stack.empty() && stack.back() == 'b' && c == 'a') {\n ++count;\n stack.pop_back();\n }\n else {\n stack.push_back(c);\n }\n }\n return count;\n }\n};",
"memory": "21070"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\n int impl(const string& s, int a_count, int i = 0, int numb = 0)\n {\n if(i >= s.size()) return 0;\n if((i+1) < s.size() && s[i] == 'b' && s[i+1] == 'a')\n {\n return std::min(\n 1 + numb + impl(s, a_count, i + 1, 0) \n , a_count\n );\n }\n a_count -= s[i] == 'a';\n numb += s[i] == 'b';\n return impl(s, a_count, i + 1, numb);\n }\npublic:\n int minimumDeletions(const string& s)\n {\n int a{0};\n for(char c : s) a += (c=='a');\n return impl(s, a, 0, 0);\n }\n};\n// aaa ba bba ba ab",
"memory": "21070"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "const char optimize = []() {std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 'c';}();\n\nclass Solution {\npublic:\n int minimumDeletions(const std::string& s) const noexcept {\n int end = s.size() - 1, start = 0;\n while(start < s.size() && s[start] == 'a') ++start;\n while(end >= 0 && s[end] == 'b') --end;\n std::stack<bool> stack; \n int count = 0;\n stack.push(s[start]);\n for(int i = start + 1; i <= end; ++i){\n if(!stack.empty() && stack.top() && s[i] == 'a'){\n stack.pop();\n ++count;\n continue;\n }\n stack.push(s[i] == 'b');\n }\n return count;\n }\n};",
"memory": "21490"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "const char optimize = []() {std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 'c';}();\n\nclass Solution {\npublic:\n int minimumDeletions(const std::string& s) const noexcept {\n int end = s.size() - 1, start = 0;\n while(start < s.size() && s[start] == 'a') ++start;\n while(end >= 0 && s[end] == 'b') --end;\n std::stack<bool> stack; \n int count = 0;\n stack.push(s[start]);\n for(int i = start + 1; i <= end; ++i){\n if(!stack.empty() && stack.top() && s[i] == 'a'){\n stack.pop();\n ++count;\n continue;\n }\n stack.push(s[i] == 'b');\n }\n return count;\n }\n};",
"memory": "21490"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "namespace constants {\nconstexpr auto max_len = 100'000;\nconstexpr auto valid_chars = \"ab\";\n} // namespace constants\n\nclass Solution {\npublic:\n auto minimumDeletions(string const &s) const -> int {\n auto const n = ssize(s);\n assert(1 <= n && n <= constants::max_len);\n assert(s.find_first_not_of(constants::valid_chars) == string::npos);\n auto t = string{};\n t.reserve(n);\n for (auto const ch : s)\n if (empty(t) || t.back() <= ch)\n t.push_back(ch);\n else\n *upper_bound(begin(t), end(t), ch) = ch;\n return static_cast<int>(n - ssize(t));\n }\n};",
"memory": "21910"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int not_flip = 0, flip = 0;\n for(char c : s) {\n int temp = not_flip + (c == 'b');\n flip = min(flip, not_flip) + (c == 'a');\n not_flip = temp;\n }\n\n return min(not_flip, flip);\n }\n};",
"memory": "22330"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int res = 0, count = 0;\n for (char c : s){\n if (c == 'b')\n count++;\n else if (count){\n res++;\n count--;\n }\n }\n return res;\n }\n};",
"memory": "22330"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int count = 0;\n int ans = 0;\n for(char& c : s)\n {\n if(c == 'b')\n count++;\n else\n ans = min(ans + 1, count);\n }\n return ans;\n }\n};",
"memory": "22750"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) \n {\n int ans = 0, countB = 0;\n for(auto ch:s)\n {\n if(ch == 'a') ans = min(ans+1, countB);\n else countB++;\n }\n return ans;\n }\n};",
"memory": "23170"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) \n {\n int ans = 0, countB = 0;\n for(auto ch:s)\n {\n if(ch == 'a') ans = min(ans+1, countB);\n else countB++;\n }\n return ans;\n }\n};",
"memory": "23170"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int total = 0;\n for(auto a : s) total += a == 'a';\n int ans = total;\n int sum = 0;\n for(int i = 0 ; i< s.size() ; ++i) {\n sum += s[i] == 'a';\n total -= s[i] == 'a';\n ans = min(ans, (i+1 - sum) + total);\n }\n return ans;\n }\n};",
"memory": "23590"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n \n int a = 0;\n int b = 0;\n\n int c,d;\n\n for(int i = 1; i <= n; ++i) {\n if (s[i-1] == 'a') {\n c = a;\n d = b + 1;\n } else {\n c = a + 1;\n d = min(a, b);\n }\n\n a = c;\n b = d;\n\n // cout << dp[i][0] << \" \" << dp[i][1] << \" \\n\";\n }\n\n return min(a, b);\n }\n};",
"memory": "23590"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n int f[n + 1];\n memset(f, 0, sizeof(f));\n int b = 0;\n for (int i = 1; i <= n; ++i) {\n if (s[i - 1] == 'b') {\n f[i] = f[i - 1];\n ++b;\n } else {\n f[i] = min(f[i - 1] + 1, b);\n }\n }\n return f[n];\n }\n};",
"memory": "24010"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<char> st; // Stack to store 'b' characters\n int deletions = 0; // Track the number of deletions\n //ab in stack this ba so if b then we push in stack if a we check top is b ? then we \n //inc del cnt and pop\n for (char c : s) \n {\n if (c == 'b') \n {\n // Push 'b' to the stack\n st.push(c);\n } else \n {\n // If we encounter 'a' and there is a 'b' in the stack\n if (!st.empty() && st.top() == 'b') \n {\n st.pop(); // Remove 'b', meaning delete 'b' or 'a'\n deletions++; // Increment the deletion count\n }\n }\n }\n \n return deletions;\n }\n};",
"memory": "24010"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<char>st;\n int del=0;\n for(int i=0;i<s.size();i++){\n if(st.empty()){\n st.push(s[i]);\n }\n else{\n if(st.top()=='a'&&s[i]=='b'){\n st.push(s[i]);\n }\n else if(st.top()=='b'&&s[i]=='a'){\n st.pop();\n del++;\n }\n else{\n st.push(s[i]);\n }\n\n }\n }\n returnΒ del;\nΒ Β Β Β }\n};\n",
"memory": "24430"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "/*Approach 4: Using stack (one pass)\nIntuition\nWhat if we focus on removing \"ba\" pairs? These pairs unbalance the string because an 'a' character is to the right of a 'b' character. \nBy leveraging a stack, we can efficiently count and remove these pairs in a single traversal of the string.\n\nTo implement this approach, we traverse the string and push each character onto the stack. When we encounter a \"ba\" pairβwhere an 'a' is \non top of the stack and a 'b' is currently being processedβwe pop the 'a' from the stack, effectively \"removing\" this out-of-order pair. \nWe keep a count of such removals throughout this process.\n\nHowever, in the worst case (when no deletions are needed), it still uses O(n) space for the stack.\n*/\nclass Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.length();\n stack<char> charStack;\n int deleteCount = 0;\n\n // Iterate through each character in the string\n for (int i = 0; i < n; i++) {\n // If stack is not empty, top of stack is 'b',\n // and current char is 'a'\n if (!charStack.empty() && charStack.top() == 'b' && s[i] == 'a') {\n charStack.pop(); // Remove 'b' from stack\n deleteCount++; // Increment deletion count\n } else {\n charStack.push(s[i]); // Push current character onto stack\n }\n }\n\n return deleteCount;\n }\n};\n/*Complexity Analysis\nLet n be the size of string s.\n\nTime complexity: O(n)\nThe algorithm performs a single linear pass over the string, with stack operations (push and pop) taking O(1) time.\n\nSpace complexity: O(n)\nThe algorithm uses a stack that may grow up to the size of the string.\n*/",
"memory": "24430"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int func(int left, int right, string &s, int &a, int &b) {\n if (left >= right) return 0;\n\n // Remove leading 'a's and trailing 'b's\n while (left <= right && s[left] == 'a') {\n a--;\n left++;\n }\n while (left <= right && s[right] == 'b') {\n b--;\n right--;\n }\n int currentCost = min(a, b);\n // Calculate minimum deletions needed\n if (left <= right) {\n // Cost of removing all 'b's up to the current index\n // and all 'a's after the current index\n \n int leftCountB = 0, rightCountA = 0;\n\n // Count consecutive 'b's from the left\n while (left <= right && s[left] == 'b') {\n leftCountB++;\n left++;\n }\n\n // Count consecutive 'a's from the right\n while (left <= right && s[right] == 'a') {\n rightCountA++;\n right--;\n }\n\n a -= rightCountA;\n b -= leftCountB;\n\n // Update the cost\n currentCost = min(currentCost, leftCountB + rightCountA + func(left, right, s, a, b));\n }\n\n return currentCost;\n }\n\n int minimumDeletions(string s) {\n int n = s.size();\n int a = 0, b = 0;\n\n // Count total 'a's and 'b's\n for (char ch : s) {\n if (ch == 'a') a++;\n else b++;\n }\n\n return func(0, n - 1, s, a, b);\n }\n};\n",
"memory": "24850"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int usingDP(string s) {\n int n = s.size();\n // int *dp = new int[n];\n int prev = 0;\n int bsSoFar = 0;\n if(s[0] == 'b') {\n // dp[0] = 0;\n prev = 0;\n bsSoFar++;\n }\n else {\n // dp[0] = 0;\n prev = 0;\n }\n for(int i=1; i<n; i++) {\n if(s[i] == 'b') {\n // dp[i] = dp[i-1];\n bsSoFar++;\n continue;\n }\n int keepA = bsSoFar;\n // int notKeepA = dp[i-1] + 1;\n int notKeepA = prev + 1;\n // dp[i] = min(keepA, notKeepA);\n prev = min(keepA, notKeepA);\n }\n // return dp[n-1];\n return prev;\n }\n\n int minimumDeletions(string s) {\n // we can delete all a's in between b's\n // or delete all b's that come in a\n // do this for every substring that contains both a and b\n return usingDP(s);\n int i=0, n = s.size();\n // int totala = 0;\n int aAfter = 0;\n while(i < n) {\n if(s[i] == 'a') aAfter++;\n i++;\n }\n i=0;\n int bBefore = 0;\n int minn = INT_MAX;\n while(i<n) {\n // int aAfter = totala - cura;\n if(s[i] == 'a')\n aAfter--; // this is done so that only a's that come after the current index are counted, not the current index if it is 'a'\n // int bBefore = i - cura; // not i+1 so that b's before this index is counted, not the current index if it is 'b'\n minn = min(minn, bBefore + aAfter);\n // at the currentindex we can place anything, as after it there are all b's, and before all a's ==> aaa..'a'bbbb.. , aaa..'b'bbbb.. --> both are satisfied\n if(s[i] == 'b')\n bBefore++; \n i++;\n }\n return minn;\n }\n};",
"memory": "27370"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n string s1=s;\n reverse(s1.begin(),s1.end());\n\n int t1=0,t2=0;\n\n for(int i=0;i<s1.size();i++)\n {\n if(s1[i]=='a')\n {\n t1++;\n }\n else if(s1[i]=='b')\n {\n if(t1>0)\n {\n --t1;\n t2++;\n }\n }\n }\n return t2;\n }\n};",
"memory": "27370"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int minDel(string s){\n stack<char> st;\n\n int ans = 0;\n\n for(auto it: s){\n if(it=='b')st.push(it);\n\n if(it=='a'){\n if(!st.empty()){ st.pop();ans++;}\n }\n }\n\n return ans;\n }\npublic:\n int minimumDeletions(string s) {\n int n = s.length();\n\n return minDel(s);\n }\n};",
"memory": "27790"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int points(string s, string maqsad) {\n int j = 0, n = s.size();\n int count = 0;\n for (int i = 0; i < n; i++) {\n s[j++] = s[i];\nif (j > 1 and s[j - 1] == maqsad[1] and s[j - 2] == maqsad[0]) {\n j -= 2;\n count++;\n }\n }\n s = s.substr(0, j);\n return count;\n }\n\n\n int minimumDeletions(string s) {\n string s1 = \"ba\";\n return points(s, s1) ;\n }\n};",
"memory": "27790"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int a=0, b=0, ans = INT_MAX;\n for(char c : s){\n if(c-'a' == 0) a++;\n else b++;\n }\n if(a==0 || b==0) return 0;\n b = 0;\n s += \"b\";\n for(char c : s){\n ans = min(ans, a+b);\n if(c-'a' == 1) b++;\n else a--;\n }\n return ans;\n }\n};",
"memory": "28210"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int a=0, b=0, ans=1e9;\n for(char c: s) if (c=='a') a++;\n s+='b';\n for(char c: s){\n if (c=='a') { \n a--;\n }\n else{\n ans=min(ans, b+a);\n b++; \n }\n }\n return ans==1e9?0:ans;\n }\n};",
"memory": "28210"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<char> st;\n for(char it: s){\n st.push(it);\n }\n int cnt =0;\n int acount = 0, bcount =0;\n while(!st.empty()){\n char front = st.top();\n st.pop();\n if(front == 'a'){\n acount++;\n }\n else{\n if(acount > 0){\n cnt++;\n acount--;\n }else{\n bcount++;\n }\n }\n }\n return cnt;\n }\n};",
"memory": "28630"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[2][100001];\n\n int solve(string& s, int flag, int pos){\n if(pos == s.size())\n return 0;\n \n auto& ans = dp[flag][pos];\n if(ans != -1)\n return ans;\n \n if(flag)\n return ans = (s[pos] == 'a') + solve(s, flag, (pos + 1));\n else{\n if(s[pos] == 'a')\n return ans = solve(s, flag, (pos + 1));\n else\n return ans = min(solve(s, !flag, (pos + 1)), (1 + solve(s, flag, (pos + 1))));\n }\n }\n int minimumDeletions(string& s) {\n memset(dp, -1, sizeof dp);\n return solve(s, 0, 0);\n }\n};",
"memory": "28630"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // using LIS of dp to solve\n int minimumDeletions(string s) {\n vector<char> temp={};\n temp.push_back(s[0]);\n int n=s.size();\n\n for(int i=1;i<n;i++){\n int x=s[i]-'a';\n int y=temp.back()-'a';\n if(x>=y){\n temp.push_back(s[i]);\n }else{\n // cout<<i<<endl;\n int ind=upper_bound(temp.begin(), temp.end(), s[i])-temp.begin();\n temp[ind]=s[i]; \n }\n }\n\n // for(auto it:temp){\n // cout<<it<<\" \";\n // }\n // cout<<endl;\n\n return n-temp.size();\n }\n};",
"memory": "29050"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int popA(string s, stack<char> stk)\n {\n int count = 0;\n int n = s.size();\n for(int i=0; i<n; i++)\n {\n if( stk.empty() )\n {\n stk.push(s[i]);\n }\n else if( !stk.empty() && s[i] == 'a' && stk.top() == 'b' )\n {\n while( !stk.empty() && s[i] == 'a' && stk.top() == 'b' )\n {\n stk.pop();\n i++;\n count++;\n }\n stk.push(s[i]);\n }\n else\n {\n stk.push(s[i]);\n }\n }\n\n return count;\n }\n\n // int popA(string s, stack<char> stk)\n // {\n // int count = 0;\n // int n = s.size();\n // for(int i=0; i<n; i++)\n // {\n // if( stk.empty() )\n // {\n // stk.push(s[i]);\n // }\n // else if( !stk.empty() && s[i] == 'a' && stk.top() == 'b' )\n // {\n // while( i < n && s[i] == 'a' )\n // {\n // i++;\n // count++;\n // }\n // stk.push(s[i]);\n // }\n // else\n // {\n // stk.push(s[i]);\n // }\n // }\n\n // return count;\n // }\n int minimumDeletions(string s) {\n stack<char> stk;\n\n int a = popA(s, stk);\n // int b = popB(s, stk);\n\n // cout<<a<<\" \"<<b;\n return a;\n }\n};",
"memory": "29050"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n stack<int>st;\n int count=0;\n for(int i=0;i<n;++i){\n if(s[i]=='a'){\n if(!st.empty() && s[st.top()]=='b'){\n count++;\n st.pop();\n }\n // st.push(i);\n }\n else{\n st.push(i);\n }\n }\n return count;\n }\n};",
"memory": "29470"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<int> st;\n int count = 0;\n for(int i = 0; i < s.size(); ++i){\n if(!st.empty() and s[i] == 'a' and s[st.top()] == 'b') st.pop(), count++;\n if(s[i] == 'b') st.push(i);\n }\n return count;\n }\n};",
"memory": "29470"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<char>st,st2;\n for(int i=0;i<s.size();i++)\n st.push(s[i]);\n int ans=0;\n while(!st.empty()){\n if(st.top()=='a')\n st2.push('a');\n else if(!st2.empty()){\n st2.pop();\n ans++;\n }\n st.pop();\n }\n return ans;\n }\n};",
"memory": "29890"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<int> st;\n int del = 0;\n for (int i=s.size()-1;i>=0;i--)\n {\n if(st.empty())\n {\n st.push(s[i]);\n }\n else\n {\n if(s[i]=='b')\n {\n if(!st.empty() && st.top()=='a')\n {\n st.pop();\n del++;\n }\n \n }\n else\n {\n st.push(s[i]);\n }\n }\n }\n \n\n\n return del;\n }\n};",
"memory": "29890"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n // int count=INT_MAX;\n int n=s.length();\n \n stack<int>countB;\n int count=0;\n for(int i=0;i<n;i++){\n if(s[i]=='a'){\n if(countB.size()>0){\n countB.pop();\n count++;\n \n }\n }\n else if(s[i]=='b'){\n countB.push(1);\n }\n }\n\n return count;\n }\n};",
"memory": "30310"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<int> st; int res = 0; \n for(int i=0;i<s.size();i++){\n if(s[i]=='b') st.push(s[i]);\n else{\n if(!st.empty()){\n res++;\n st.pop();\n }\n }\n }\n return res;\n }\n};",
"memory": "30310"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[100001][2];\n int solve(int i, string &s, char prev){\n if(i>=s.size()) return 0;\n\n //curr delete when prev b and curr a delete mendetory\n int del = INT_MAX;\n int keep = INT_MAX; \n char curr = s[i];\n\n if(dp[i][prev-'a']!=-1) return dp[i][prev-'a'];\n\n if(prev == 'a' && curr == 'b'){\n //both op\n del = 1 + solve(i+1, s, prev);\n keep = solve(i+1, s, curr);\n\n }else if(prev == curr){\n //only keep\n keep = min(keep, solve(i+1, s, curr));\n \n }else{\n //only delete\n del = min(del, 1+solve(i+1, s, prev));\n }\n return dp[i][prev-'a'] = min(keep, del);\n }\n int minimumDeletions(string s) {\n memset(dp, -1, sizeof(dp));\n return solve(0, s, 'a');\n }\n};",
"memory": "30730"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[100001][2];\n int fun(string&s,int cur,int i){\n if(i==s.length()) return 0;\n int ans=0;\n if(dp[i][cur]!=-1) return dp[i][cur];\n if(cur==0){\n if(s[i]=='a') ans=fun(s,cur,i+1);\n else{\n ans=min(1+fun(s,cur,i+1),fun(s,1,i+1));\n }\n }\n else{\n if(s[i]=='b') ans=fun(s,cur,i+1);\n else{\n ans=1+fun(s,cur,i+1);\n }\n }\n return dp[i][cur]=ans;\n }\n int minimumDeletions(string s) {\n memset(dp,-1,sizeof(dp));\n return fun(s,0,0);\n }\n};",
"memory": "30730"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[100005][3];\n int solve(int ind, int mode, string &s){\n // base case \n if(ind == s.size()) return 0;\n // rec case \n int ans = INT_MAX;\n\n if(dp[ind][mode] != -1) return dp[ind][mode];\n\n if(mode == 0){\n if(s[ind] == 'b'){\n ans = min(ans,1+solve(ind+1,0,s));\n ans = min(ans,solve(ind,1,s));\n }else{\n ans = min(ans,solve(ind+1,0,s));\n }\n }else{\n if(s[ind] == 'a'){\n ans = min(ans,1+solve(ind+1,1,s));\n }else{\n ans = min(ans,solve(ind+1,1,s));\n }\n\n }\n\n return dp[ind][mode] = ans;\n\n\n }\n int minimumDeletions(string s) {\n memset(dp,-1,sizeof(dp));\n return solve(0,0,s); \n }\n};",
"memory": "31150"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[100005][2];\n int n;\n string s;\n\n int solve(int idx, bool deletedAllB) {\n if (idx >= n)\n return 0;\n\n int& ret = dp[idx][deletedAllB];\n if (~ret)\n return ret;\n\n if (s[idx] == 'a') {\n int i = idx;\n while (i < n && s[i] == 'a')\n i++;\n if (deletedAllB) {\n ret = solve(i, deletedAllB);\n } else {\n int seg = i - idx;\n ret = solve(i, deletedAllB) + seg;\n }\n } else {\n int i = idx;\n while (i < n && s[i] == 'b')\n i++;\n if (i == n) {\n ret = 0;\n } else {\n int seg = i - idx;\n if(deletedAllB){\n ret = solve(i, false);\n ret = min(ret, solve(i, true) + seg);\n }else{\n ret = solve(i, false);\n }\n }\n }\n return ret;\n }\n\n int minimumDeletions(string str) {\n n = (int)str.size();\n s = str;\n memset(dp, -1, sizeof(dp));\n\n int ans = solve(0, true);\n\n return ans;\n }\n};",
"memory": "31150"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void clean(string& s, int& countA, int& countB){\n if(s.length() == 0){\n return;\n }\n int start = 0;\n while(s.length() > 0 && s.front() == 'a'){\n countA--;\n s.erase(s.begin());\n }\n while(s.length() > 0 && s.back() == 'b'){\n countB--;\n s.pop_back();\n }\n }\n\n int minimumDeletionsUtils(string s, int countA, int countB){\n clean(s, countA, countB);\n //cout << \" checking for \" << s << \" \" << countA << \" \" << countB << endl;\n if(s.length() == 0){\n return 0;\n }\n \n\n int ans = INT_MAX;\n //cout << \"checking first \" << endl;\n if(countB <= countA){\n ans = min(ans, 1 + minimumDeletionsUtils(s.substr(1), countA, countB-1));\n }\n else{\n ans = min(ans, 1 + minimumDeletionsUtils(s.substr(0, s.length()-1), countA-1, countB));\n }\n return ans;\n }\n\n int counter(string s, char c){\n int ans = 0;\n for(int i = 0; i < s.length(); i++){\n if(s[i] == c)\n ans++;\n }\n return ans;\n }\n int minimumDeletions(string s) {\n //cout << minimumDeletionsUtils(s) << endl;\n //map<string, int> record;\n int countA = counter(s, 'a');\n int countB = counter(s, 'b');\n //return minimumDeletionsUtils(s, countA, countB);\n\n int ans = 0;\n while(s.length() > 0){\n clean(s, countA, countB);\n if(s.length() == 0){\n return ans;\n }\n ans++;\n if(countB <= countA){\n s.erase(s.begin());\n countB--;\n }else{\n s.pop_back();\n countA--;\n }\n }\n\n return ans;\n }\n};",
"memory": "31570"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void clean(string& s, int& countA, int& countB){\n if(s.length() == 0){\n return;\n }\n int start = 0;\n while(s.length() > 0 && s.front() == 'a'){\n countA--;\n s.erase(s.begin());\n }\n while(s.length() > 0 && s.back() == 'b'){\n countB--;\n s.pop_back();\n }\n }\n\n int counter(string s, char c){\n int ans = 0;\n for(int i = 0; i < s.length(); i++){\n if(s[i] == c)\n ans++;\n }\n return ans;\n }\n int minimumDeletions(string s) {\n\n int countA = counter(s, 'a');\n int countB = counter(s, 'b');\n\n int ans = 0;\n while(s.length() > 0){\n clean(s, countA, countB);\n if(s.length() == 0){\n return ans;\n }\n ans++;\n if(countB <= countA){\n s.erase(s.begin());\n countB--;\n }else{\n s.pop_back();\n countA--;\n }\n }\n\n return ans;\n }\n};",
"memory": "31570"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n \n int dp[100001][2];\n string s=\"\";\n int n;\n int solve(int i, int t) \n {\n if(i==n) return 0;\n \n if(dp[i][t]!=-1) return dp[i][t];\n int ans=0;\n if(t)// their is a previous b present \n {\n if(s[i]=='b')// no need to erase this one as we have kept a previous b so it does't matter to remove current one bcz u already have one b some where \n {\n ans= solve(i+1,t);\n }else ans= solve(i+1,t)+1;// remove any a\n }else\n {\n if(s[i]=='b')\n {\n ans= solve(i+1, 1);// keep it // can keep it so t=1\n ans= min(ans, solve(i+1,0)+1);// remove it so t=0\n }else\n {\n ans= solve(i+1,t); // no need to remove any a if no previous b is present \n }\n }\n return dp[i][t]=ans;\n }\n \n int minimumDeletions(string s1) {\n s=s1;\n n=s.size();\n memset(dp,-1,sizeof(dp));\n return solve(0,0);\n }\n};\n",
"memory": "34510"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution \n{\npublic:\n int minimumDeletions(string s) \n {\n int n=s.size(),nb,na,del_b=0,del_a=0;\n stack<pair<int,int>> stack;\n for(int i=0;i<n;)\n {\n na=0;\n nb=0;\n while(i<n && s[i]=='b')\n {\n nb++;\n i++;\n }\n while(i<n && s[i]=='a')\n {\n na++;\n i++;\n }\n del_b+=nb;\n stack.push({na,nb});\n }\n int min_del=del_b;\n pair<int,int> undo_del;\n while(!stack.empty())\n {\n undo_del=stack.top();\n stack.pop();\n del_a+=undo_del.first;\n del_b-=undo_del.second;\n min_del=min(min_del,del_a+del_b);\n }\n return min_del; \n }\n};",
"memory": "34510"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n stack<int>st;\n int n = s.size();\n int c1 = 0;\n for(int i=0;i<n;i++){\n if(s[i]=='b'){\n st.push(i);\n }\n else{\n if(!st.empty()){\n st.pop();\n c1++;\n }\n }\n }\n while(!st.empty()){\n st.pop();\n }\n int c2 = 0;\n for(int i=n-1;i>=0;i--){\n if(s[i]=='a'){\n st.push(i);\n }\n else{\n if(!st.empty()){\n st.pop();\n c2++;\n }\n }\n }\n return min(c1,c2);\n }\n};",
"memory": "34930"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\nint pre[1303207], suf[1303207];\n\npublic:\n int minimumDeletions(string s) {\n int ans = 1e9 + 7;\n\n int ca =0, cb = 0;\n\n\n for (int i = 0; i < s.size(); i++){\n pre[i] = cb;\n if(s[i] == 'b') cb++;\n }\n\n\n for (int i = s.size() - 1; i >= 0; i--){\n suf[i] = ca;\n if(s[i] == 'a') ca++;\n }\n\n\n for (int i = 0; i < s.size(); i++){\n ans = min(ans, pre[i] + suf[i]);\n }\n\n\n return ans;\n }\n};",
"memory": "34930"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solve(string &s,int &n,int i,bool flag,int dp[][2]){\n if(i==n) return 0;\n if(dp[i][flag]!=-1) return dp[i][flag];\n int skip=1+solve(s,n,i+1,flag,dp),take=n;\n if(s[i]=='a' && flag) take=solve(s,n,i+1,true,dp);\n else if(s[i]=='b') take=solve(s,n,i+1,false,dp);\n return dp[i][flag]=min(skip,take);\n }\n int minimumDeletions(string s) {\n int n=s.size(),dp[n][2];\n memset(dp,-1,sizeof dp);\n return solve(s,n,0,true,dp);\n }\n};",
"memory": "35350"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n static const int N = 2e5+7, inf = 1e9+7;\nint dp[N][2];\nstring s;\nint f(int i,int ok){\n if(i == s.size()) return 0;\n if(dp[i][ok] != -1) return dp[i][ok];\n int ans = inf;\n if(s[i] == 'a'){\n if(!ok) ans = min(ans,f(i+1,ok));\n else ans = min(ans,1 + f(i+1,ok));\n }\n else{\n if(!ok) {\n ans = min(ans,f(i+1,1));\n ans = min(ans,1+f(i+1,ok));\n }\n else{\n ans = min(ans,f(i+1,ok));\n }\n }\n return dp[i][ok] = ans;\n}\n int minimumDeletions(string ss) {\n s = ss;\n memset(dp,-1,sizeof dp);\n return f(0,0);\n }\n};",
"memory": "35350"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n /*int print0(string &b,int c,int e,char temp0){\n if(c==e){\n return 0;\n }\n int temp2=0;\n if(temp0=='a'){\n if(b[c]=='a'){\n temp2=temp2+print0(b,c+1,e,'a');\n }\n else if(b[c]=='b'){\n temp2=min(print0(b,c+1,e,'a')+1,print0(b,c+1,e,'b'));\n }\n }\n else if(temp0=='b'){\n if(b[c]=='a'){\n temp2=print0(b,c+1,e,'b')+1;\n }\n else if(b[c]=='b'){\n temp2=print0(b,c+1,e,'b');\n }\n }\n return temp2;\n }*/\n int print0(string &b,int c,int e,/*vector<vector<int>> &temp0*/int temp0[100002][2],char temp2){\n if(c==e){\n return 0;\n }\n int temp12=0;\n if(temp2=='a'){\n if(temp0[c][0]!=-1){\n return temp0[c][0];\n }\n }\n else if(temp2=='b'){\n if(temp0[c][1]!=-1){\n return temp0[c][1];\n }\n }\n if(temp2=='a'){\n if(b[c]=='a'){\n temp12=print0(b,c+1,e,temp0,'a');\n }\n else if(b[c]=='b'){\n temp12=min(print0(b,c+1,e,temp0,'a')+1,print0(b,c+1,e,temp0,'b'));\n }\n }\n else if(temp2=='b'){\n if(b[c]=='a'){\n temp12=print0(b,c+1,e,temp0,'b')+1;\n }\n else if(b[c]=='b'){\n temp12=print0(b,c+1,e,temp0,'b');\n }\n }\n if(temp2=='a'){\n temp0[c][0]=temp12;\n }\n else if(temp2=='b'){\n temp0[c][1]=temp12;\n }\n return temp12;\n }\n int minimumDeletions(string s) {\n /*int b=0;\n int c=s.size();\n string temp0=s;\n char temp2='a';\n int e=0;\n e=print0(temp0,b,c,temp2);\n return e;*/\n int b=0;\n int c=s.size();\n string temp0=s;\n char temp2='a';\n //vector<vector<int>> temp12;\n int temp12[100002][2];\n for(int temp18=0;temp18<s.size();temp18++){\n //vector<int> temp28;\n for(int temp48=0;temp48<2;temp48++){\n //temp28.push_back(-1);\n temp12[temp18][temp48]=-1;\n }\n //temp12.push_back(temp28);\n }\n int temp18=0;\n temp18=print0(temp0,b,c,temp12,temp2);\n return temp18;\n }\n};",
"memory": "35770"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int approach_1(string s){\n\n int n = s.size(); int count = 0;\n\n stack<int> st;\n for(int i = 0; i<n; i++){\n if(!st.empty() && s[i] == 'a' && st.top() == 'b'){\n st.pop();\n count++;\n }\n else{\n st.push(s[i]);\n }\n }\n\n return count;\n }\n int approach_2(string s){\n \n int n = s.size();\n vector<int> count_b(n, 0);\n for(int i = 1; i<n; i++){ // 1 -> n-1\n count_b[i] = count_b[i-1];\n if(s[i-1] == 'b'){\n count_b[i] += 1;\n }\n }\n\n vector<int> count_a(n, 0); // 0 <- n-2 \n for(int i = n-2; i >= 0; i--){\n\n count_a[i] = count_a[i+1];\n\n if(s[i+1] == 'a') {\n count_a[i] += 1;\n }\n }\n\n int res = INT_MAX;\n\n for(int i = 0; i<n; i++){\n res = min(res, count_a[i] + count_b[i]);\n }\n\n return res;\n }\n\n int optimized(string s){\n int n = s.size();\n\n int count_a = 0;\n\n for(int i = n-1; i >= 0; i--){\n if(s[i] == 'a'){\n count_a++;\n }\n }\n\n int count = INT_MAX;\n int count_b = 0;\n\n for(int i = 0; i< n; i++){\n if(s[i] == 'a'){\n count_a--;\n }\n\n count = min(count, count_b + count_a);\n\n if(s[i] == 'b'){\n count_b++;\n }\n }\n\n return count;\n }\n int minimumDeletions(string s) {\n // return optimized(s);\n return approach_1(s);\n }\n};",
"memory": "36190"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // int getA(string s, int index) {\n // int count = 0;\n // for (int i = index; i < s.length(); i++) {\n // if (s[i] == 'a') {\n // count++;\n // }\n // }\n // return count;\n // }\n // int solve(string& s, int index) {\n\n // if (index == s.size())\n // return 0;\n // int ans = 0;\n // // int dela =INT_MAX;\n\n // if (s[index] == 'b') {\n // int del = 1 + solve(s, index + 1);\n // int notdel = getA(s, index);\n // ans = min(del, notdel);\n // } else\n // return solve(s, index + 1);\n // // else {\n // // // if(s[index] == 'a')\n // // dela = getA(s,index);\n // // }\n // // return min(dela, ans)\n // return ans;\n // }\n\n int solveDp(string& s, int index, vector<int>& dp , vector<int> &a) {\n\n if (index == s.size())\n return 0;\n\n if (dp[index] != -1)\n return dp[index];\n\n int ans = 0;\n \n if (s[index] == 'b') {\n int del = 1 + solveDp(s, index + 1, dp,a);\n int notdel = a[index];\n ans = min(del, notdel);\n } else\n return solveDp(s, index + 1, dp,a);\n\n return dp[index] = ans;\n }\n\n int stackSol(string s){\n \n stack<int> st;\n // st.push(s[0]);\n int ans = 0;\n\n // for(int i= 1; i<s.length() ; i++){\n // char ch = s[i];\n // if(ch =='a'){\n // int top = st.top();\n // if(top =='a'){\n // continue;\n // }\n // else{\n // // top == b\n // ans++;\n // st.pop();\n // st.push(ch);\n // }\n // }\n // else{\n // // ch == 'b'\n // int top = st.top();\n // if(top == 'a'){\n // st.push(ch);\n // continue;\n // }\n // else{\n // // top == b\n // continue;\n // }\n // }\n \n // }\n // return ans;\n\n for(char ch : s){\n if(ch == 'a'){\n if(!st.empty() && st.top() == 'b'){\n st.pop();\n ans++;\n }\n else{\n st.push(ch);\n }\n }\n else{\n st.push(ch);\n }\n }\n return ans;\n }\n\n // int solveTab(string& s) {\n \n // vector<int> dp(s.length() + 1, 0);\n\n // for (int index = s.length() - 1; index >= 0; index--) {\n // int ans = 0;\n // // int dela =INT_MAX;\n\n // if (s[index] == 'b') {\n // int del = 1 + dp[index + 1];\n // int notdel = getA(s, index);\n // ans = min(del, notdel);\n // } else\n // return dp[index + 1];\n\n // dp[index] = ans;\n // }\n // return dp[0];\n // }\n\n\n int minimumDeletions(string s) {\n // int n = s.length();\n // int i = 0;\n // while (s[i] == 'a') {\n // i++;\n // }\n // if (i == s.length()) {\n // return 0;\n // }\n\n // vector<int> dp(n + 1, -1);\n // vector<int> a(n, 0);\n // if(s[n-1] == 'a'){\n // a[n-1] = 1 ;\n // }\n // else{\n // a[n-1] = 0;\n // }\n // for(int i = n-2 ; i>= 0 ; i--){\n // if(s[i] != 'a'){\n // a[i] = a[i+1];\n // }\n // else{\n // a[i] = a[i+1] +1 ;\n // }\n // }\n\n // return solveDp(s, i, dp,a);\n // // return solveTab(s);\n // }\n \n return stackSol(s);\n }\n};",
"memory": "36610"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // int getA(string s, int index) {\n // int count = 0;\n // for (int i = index; i < s.length(); i++) {\n // if (s[i] == 'a') {\n // count++;\n // }\n // }\n // return count;\n // }\n // int solve(string& s, int index) {\n\n // if (index == s.size())\n // return 0;\n // int ans = 0;\n // // int dela =INT_MAX;\n\n // if (s[index] == 'b') {\n // int del = 1 + solve(s, index + 1);\n // int notdel = getA(s, index);\n // ans = min(del, notdel);\n // } else\n // return solve(s, index + 1);\n // // else {\n // // // if(s[index] == 'a')\n // // dela = getA(s,index);\n // // }\n // // return min(dela, ans)\n // return ans;\n // }\n\n int solveDp(string& s, int index, vector<int>& dp , vector<int> &a) {\n\n if (index == s.size())\n return 0;\n\n if (dp[index] != -1)\n return dp[index];\n\n int ans = 0;\n \n if (s[index] == 'b') {\n int del = 1 + solveDp(s, index + 1, dp,a);\n int notdel = a[index];\n ans = min(del, notdel);\n } else\n return solveDp(s, index + 1, dp,a);\n\n return dp[index] = ans;\n }\n\n int stackSol(string s){\n \n stack<int> st;\n st.push(s[0]);\n int ans = 0;\n\n for(int i= 1; i<s.length() ; i++){\n char ch = s[i];\n if(ch =='a' && !st.empty()){\n char top = st.top();\n if(top =='a'){\n st.push(ch);\n }\n else{\n // top == b\n ans++;\n st.pop();\n }\n }\n else{\n // ch == 'b'\n if(!st.empty() && st.top() == 'a'){\n st.push(ch);\n }\n else{\n // top == b\n st.push(ch);\n }\n }\n \n }\n return ans;\n\n // for(char ch : s){\n // if(ch == 'a'){\n // if(!st.empty() && st.top() == 'b'){\n // st.pop();\n // ans++;\n // }\n // else{\n // st.push(ch);\n // }\n // }\n // else{\n // st.push(ch);\n // }\n // }\n // return ans;\n }\n\n // int solveTab(string& s) {\n \n // vector<int> dp(s.length() + 1, 0);\n\n // for (int index = s.length() - 1; index >= 0; index--) {\n // int ans = 0;\n // // int dela =INT_MAX;\n\n // if (s[index] == 'b') {\n // int del = 1 + dp[index + 1];\n // int notdel = getA(s, index);\n // ans = min(del, notdel);\n // } else\n // return dp[index + 1];\n\n // dp[index] = ans;\n // }\n // return dp[0];\n // }\n\n\n int minimumDeletions(string s) {\n // int n = s.length();\n // int i = 0;\n // while (s[i] == 'a') {\n // i++;\n // }\n // if (i == s.length()) {\n // return 0;\n // }\n\n // vector<int> dp(n + 1, -1);\n // vector<int> a(n, 0);\n // if(s[n-1] == 'a'){\n // a[n-1] = 1 ;\n // }\n // else{\n // a[n-1] = 0;\n // }\n // for(int i = n-2 ; i>= 0 ; i--){\n // if(s[i] != 'a'){\n // a[i] = a[i+1];\n // }\n // else{\n // a[i] = a[i+1] +1 ;\n // }\n // }\n\n // return solveDp(s, i, dp,a);\n // // return solveTab(s);\n // }\n \n return stackSol(s);\n }\n};",
"memory": "36610"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "#define ll long long\n#define sz(x) (ll)x.size()\n#define INF 1e9\n\nclass Solution {\n string s;\n ll dp[100005][2];\npublic:\n\n int minimumDeletions(string ss) {\n s = ss;\n memset(dp, -1, sizeof(dp));\n return rec(0, 0);\n }\nprivate:\n\nll rec(ll idx, bool f){\n if (idx == sz(s)) return 0;\n\n ll &ret = dp[idx][f];\n if (~ret) return ret;\n\n ret = INF;\n\n if (!f){\n ret = min(ret, abs('a' - s[idx]) + rec(idx + 1, f));\n ret = min(ret, abs(('b') - s[idx]) + rec(idx + 1, 1));\n }else{\n ret = min(ret, abs(('b') - s[idx]) + rec(idx + 1, f));\n } \n\n return ret;\n}\n\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "37030"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "#define ll long long\n#define sz(x) (ll)x.size()\n#define INF 1e9\n\nclass Solution {\n string s;\n ll dp[100005][2];\npublic:\n\n int minimumDeletions(string ss) {\n s = ss;\n memset(dp, -1, sizeof(dp));\n return rec(0, 0);\n }\nprivate:\n\nll rec(ll idx, bool f){\n if (idx == sz(s)) return 0;\n\n ll &ret = dp[idx][f];\n if (~ret) return ret;\n\n ret = INF;\n\n if (!f){\n ret = min(ret, abs('a' - s[idx]) + rec(idx + 1, f));\n ret = min(ret, abs(('b') - s[idx]) + rec(idx + 1, 1));\n }else{\n ret = min(ret, abs(('b') - s[idx]) + rec(idx + 1, f));\n } \n\n return ret;\n}\n\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "37030"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n // 3 passes method time complexity O(n)\n // int n = s.length();\n // vector<int>countA(n, 0);\n // vector<int>countB(n, 0);\n // int b = 0;\n // // Count the number of B on the left of the current element\n // for(int i=0; i<n; i+=1){\n // countB[i]=b;\n // if(s[i]=='b'){\n // b+=1;\n // }\n // }\n // int a = 0;\n // // Count the number of A on the right of the current element\n // for(int i=n-1; i>=0; i-=1){\n // countA[i]=a;\n // if(s[i]=='a'){\n // a+=1;\n // }\n // }\n // int minDeletions = n;\n // // Number of deletions = num of Bs on the left of the current element + num of As on the right of the current element\n // for(int i=0; i<n; i+=1){\n // minDeletions = min(minDeletions, countA[i]+countB[i]);\n // }\n // return minDeletions;\n\n int n = s.length();\n // Store the minimum deletions needed to balance the substrings\n vector<int>dp(n+1, 0);\n int b = 0;\n for(int i=0; i<n; i+=1){\n // Keep the number of characters to be deleted if it's b\n if(s[i]=='b'){\n dp[i+1]=dp[i];\n b+=1;\n }\n // Remove a case dp[i]+1, otherwise remove all b before the current a\n else{\n dp[i+1]=min(dp[i]+1, b);\n }\n }\n return dp[n];\n }\n};",
"memory": "37450"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n vector<int> dp(n + 1, 0);\n int bCount =\n 0; // To keep track of the number of 'b's encountered so far\n\n for (int i = 1; i <= n; i++) {\n if (s[i - 1] == 'b') {\n bCount++;\n dp[i] = dp[i - 1]; // No need to delete 'b', just carry forward\n // the previous value\n } else {\n // If it's 'a', we have two choices:\n // 1. Delete this 'a', so we add 1 to the previous value\n // (dp[i-1] + 1)\n // 2. Keep this 'a', but we need to delete all 'b's encountered\n // before it (bCount)\n dp[i] = min(dp[i - 1] + 1, bCount);\n }\n }\n\n return dp[n];\n }\n};",
"memory": "37870"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n // a a b a b b a b\n // \n int n = s.length();\n vector<int> rightA(n, 0);\n int countA = 0;\n int minFlip = INT_MAX;\n for(int i = s.length() - 1; i >= 0; i--)\n {\n rightA[i] = countA;\n if(s[i] == 'a') countA++;\n }\n int bA = 0, bB = 0, aA = rightA[0], aB = 0;\n if(s[0] == 'a')\n {\n aA = rightA[0] + 1;\n }\n minFlip = std::min(minFlip, bB + aA);\n for(int i = 0; i < n; i++)\n {\n aA = rightA[i];\n if(s[i] == 'a')\n {\n bA++;\n }\n bB = (i + 1) - bA;\n aB = (n - i - 1) - aA;\n //int tempFlip = std::min(bA + aB, bB + aA);\n minFlip = std::min(minFlip, bB + aA);\n }\n return minFlip;\n\n }\n};",
"memory": "38290"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n// int minimumDeletions(string s) {\n// int bCount = 0;\n// int result = 0;\n// char* arr = s.c_str();\n\n// for(char ch:arr) {\n// if (ch == 'b') bCount++;\n// else if (bCount == 0) continue;\n// else {\n// bCount--;\n// result++;\n// }\n// }\n\n// return result;\n// }\n\n// }\n// };\n \n// int minimumDeletions(string s){\n// int n=s.length();\n// int count=0;\n// stack<char> st;\n// for(int i=0;i<n;i++){\n// if(!st.empty()&&s[i]=='a'&&st.top()=='b'){ // 'ba'\n// st.pop();\n// count++;\n\n// }else{\n// st.push(s[i]);\n// }\n// }\n// return count;\n// }\n// };\n\n// int minimumDeletions(string s){\n// int n=s.length();\n// vector<int> left_b(n,0);\n// vector<int> right_a(n,0);\n\n// int countb=0;\n// for(int i=0;i<n;i++){\n// left_b[i]=countb;\n// if(s[i]=='b')\n// countb++;\n// }\n// int counta=0;\n// for(int i=n-1;i>=0;i--){\n// right_a[i]=counta;\n// if(s[i]=='a')\n// counta++;\n// }\n// int count=INT_MAX;\n// for(int i=0;i<n;i++){\n// count=min(count,left_b[i]+right_a[i]);\n// }\n// return count;\n// }\n// };\n\n int minimumDeletions(string s){\n int n=s.length();\n vector<int> right_a(n,0);\n\n int counta=0;\n for(int i=n-1;i>=0;i--){\n right_a[i]=counta;\n if(s[i]=='a')\n counta++;\n }\n int count=INT_MAX;\n int countb=0;\n for(int i=0;i<n;i++){\n count=min(count,countb +right_a[i]);\n if(s[i]=='b')\n countb++;\n }\n return count;\n }\n};",
"memory": "38290"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "static auto _ = []() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.length();\n vector<int> dp(n + 1, 0);\n int b_count = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 'b') {\n dp[i + 1] = dp[i];\n b_count++;\n } else {\n dp[i + 1] = min(dp[i] + 1, b_count);\n }\n }\n return dp[n];\n }\n};",
"memory": "38710"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) \n {\n int n = s.size();\n int suffAs[100001] = {0};\n\n for(int i=n-1;i>=0;i--)\n {\n suffAs[i] += suffAs[i+1];\n if(s[i] == 'a')\n suffAs[i]++;\n }\n\n vector<int> nextB(n, n);\n int prev = n;\n for(int i=n-1;i>=0;i--)\n {\n nextB[i] = prev;\n if(s[i] == 'b')\n prev = i;\n }\n\n int ans = INT_MAX;\n int cntBs = 0;\n for(int i=0;i<n;i++)\n {\n if(s[i] == 'b')\n {\n ans = min(ans, cntBs + suffAs[i]);\n cntBs++;\n if(suffAs[i] != 0)\n ans = min(ans, cntBs + suffAs[nextB[i]]);\n }\n // cout<<ans<<\" \";\n }\n if(ans == INT_MAX)\n return 0;\n return ans;\n }\n};",
"memory": "38710"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int result = 1e9;\n\n void getMin(vector <int> &prefixSum, string &s, int i, int ctB, int currSum) {\n if(i == s.length()) {\n result = min(result, currSum);\n return;\n }\n if(s[i] == 'b') {\n ctB++;\n getMin(prefixSum, s, i+1, ctB, currSum);\n }\n else {\n if(s[i] == 'a' && ctB) {\n getMin(prefixSum, s, i+1, 0, currSum+ ctB);\n result = min(result, currSum + prefixSum[i]);\n } else {\n getMin(prefixSum, s, i+1, 0, currSum);\n }\n }\n }\n\n int minimumDeletions(string s) {\n vector <int> prefixSum(s.length()+1);\n for(int i = s.length()-1;i>=0;i--) {\n prefixSum[i] = (s[i]=='a')? prefixSum[i+1]+1: prefixSum[i+1];\n }\n getMin(prefixSum, s, 0, 0, 0);\n return result;\n }\n};",
"memory": "39130"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n;\n vector<int> dp;\n \n int f(int i, int &cntA, string& s) {\n if(i==n) return 0;\n //if (dp[i] != -1) return dp[i];\n int ans=f(i+1, cntA, s);\n \n if (s[i]=='a') cntA++;\n else ans=min(ans+1, cntA);\n \n return ans;\n }\n \n int minimumDeletions(string& s) {\n n = s.size();\n dp.assign(n, -1); \n int cntA=0;\n return f(0, cntA, s);\n }\n};\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "39130"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int minDeletions;\n string s;\n void updateChar(vector<int>& charCount,int start, int end, int increment, char ch, bool secondPass){\n int currCount = 0;\n for(int i = start; i != end; i += increment){\n if(s[i - increment] == ch) currCount++;\n charCount[i] += currCount;\n if(secondPass) minDeletions = min(minDeletions, charCount[i]);\n }\n }\npublic:\n int minimumDeletions(string str) {\n s = str;\n if(s.length() < 2) return 0;\n vector<int> charCount(s.length());\n minDeletions = INT_MAX;\n \n updateChar(charCount, 1, s.length(), 1, 'b', false);\n updateChar(charCount, s.length() - 2, -1, -1, 'a', true);\n minDeletions = min(minDeletions, charCount[s.length() - 1]);\n return minDeletions;\n }\n};",
"memory": "42070"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int minDeletions;\n string s;\n void updateChar(vector<int>& charCount,int start, int end, int increment, char ch, bool secondPass){\n int currCount = 0;\n for(int i = start; i != end; i += increment){\n if(s[i - increment] == ch) currCount++;\n charCount[i] += currCount;\n if(secondPass) minDeletions = min(minDeletions, charCount[i]);\n }\n }\npublic:\n int minimumDeletions(string str) {\n s = str;\n if(s.length() < 2) return 0;\n vector<int> charCount(s.length());\n \n minDeletions = INT_MAX;\n updateChar(charCount, 1, s.length(), 1, 'b', false);\n updateChar(charCount, s.length() - 2, -1, -1, 'a', true);\n minDeletions = min(minDeletions, charCount[s.length() - 1]);\n return minDeletions;\n }\n};",
"memory": "42070"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n=s.size();\n vector<int>dp(n+1,0);\n s='#'+s;\n int cnt1=0;\n for(int i=1;i<=n;i++){\n if(s[i]=='a'){\n dp[i]=max(1+cnt1,dp[i-1]);\n cnt1++;\n }\n else\n dp[i]=1+dp[i-1];\n }\n return n-dp[s.size()-1];\n }\n};",
"memory": "42490"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int tab(string s)\n {\n vector<int> dp(s.size()+1, 0);\n int b = 0;\n\n for (int i = 0; i < s.size(); i++) \n {\n if (s[i] == 'a') \n {\n dp[i + 1] = min(dp[i] + 1, b);\n } \n else \n {\n dp[i + 1] = dp[i];\n b++;\n }\n }\n \n return dp[s.size()];\n }\n\n int rec(int i, int b, string s)\n {\n if(i == s.size())\n {\n return 0;\n }\n\n if(s[i] == 'a')\n {\n int val = rec(i+1, b, s);\n return min(val+1, b);\n }\n else\n {\n return rec(i+1, b+1, s);\n }\n }\n\n int minimumDeletions(string s) {\n\n //return rec(0, 0, s);\n return tab(s);\n \n }\n};",
"memory": "42490"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n ios::sync_with_stdio(false);\n s += 'b';\n int n = s.size();\n vector <int> suf(n);\n suf[n - 1] = (s[n - 1] == 'a');\n for(int i = n - 2; i >= 0; i--){\n suf[i] = suf[i + 1] + (s[i] == 'a');\n }\n int prev = 0, ans = INT_MAX;\n for(int i = 0; i < n; i++){\n if(s[i] == 'b'){\n ans = min(ans, prev + suf[i]);\n prev++;\n }\n }\n return ans; \n }\n};",
"memory": "42910"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) \n {\n int n=s.length(),mini=INT_MAX;\n vector<int> A(n+1,0); \n s+='b';\n\n for(int i=n-1;i>=0;--i)\n {\n A[i]=A[i+1];\n if(s[i]=='a')\n A[i]++;\n }\n \n\n int bcount=0;\n\n for(int i=0;i<=n;++i)\n {\n if (s[i]=='b')\n {\n mini=min(mini,A[i]+bcount);\n bcount++;\n }\n }\n\n return mini;\n }\n};",
"memory": "42910"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[27][100001];\n int helpme(string &s ,int n ,char prv , int index ){\n if(index>=n)return 0;\n if(dp[prv-'a'][index]!=-1)return dp[prv-'a'][index];\n int len = 0;\n if(s[index] >= prv){\n len = max(len,1+helpme(s,n,s[index] , index+1));\n }\n len = max(len , helpme(s,n,prv,index+1));\n return dp[prv-'a'][index]=len;\n }\n int minimumDeletions(string s) {\n memset(dp,-1,sizeof(dp));\n int n = s.length();\n return n-helpme(s,n,'a',0);\n \n }\n};",
"memory": "43330"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[100005];\n int func(int indx, vector<int>& suff, string& s){\n if (indx == s.size()) return 0;\n if (dp[indx]!=-1) return dp[indx];\n if (s[indx] == 'a') return dp[indx] = func(indx+1,suff,s);\n else{\n return dp[indx] = min(suff[indx],1 + func(indx+1,suff,s));\n }\n }\n int minimumDeletions(string s) {\n int n = s.size();\n vector<int> suff(n+5);\n int sum = 0;\n for (int g=n-1; g>=0; g--){\n sum+=(s[g] == 'a') ? 1 : 0;\n suff[g] = sum;\n }\n memset(dp,-1,sizeof(dp));\n return func(0,suff,s);\n }\n};",
"memory": "43330"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int count = INT_MAX;\n int a_count = 0;\n int b_count = 0;\n vector<int> curr_a;\n\n for (char c : s) {\n if (c == 'a') {\n a_count++;\n } else if (c == 'b') {\n curr_a.push_back(a_count);\n b_count++;\n }\n }\n if (a_count == 0 || b_count == 0) {\n return 0;\n }\n curr_a.push_back(a_count);\n for (int i = 0; i <= b_count; i++) {\n count = min(count, a_count - curr_a[i] + i);\n }\n return count;\n }\n};",
"memory": "43750"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int count = INT_MAX;\n int a_count = 0;\n int b_count = 0;\n vector<int> curr_a;\n\n for (char c : s) {\n if (c == 'a') {\n a_count++;\n } else if (c == 'b') {\n curr_a.push_back(a_count);\n b_count++;\n }\n }\n if (a_count == 0 || b_count == 0) {\n return 0;\n }\n curr_a.push_back(a_count);\n for (int i = 0; i <= b_count; i++) {\n count = min(count, a_count - curr_a[i] + i);\n }\n return count;\n }\n};",
"memory": "43750"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) \n {\n int n=s.size();\n // unordered_map<int,unordered_map<int,int>>m;\n // return n-f(0,-1,s,m);\n vector<int>dp;\n int idx=0;\n for(int i=0;i<n;i++)\n {\n int x=((s[i]=='a')?1:2);\n if(idx==0||x>=dp[idx-1])\n {\n dp.push_back(x);\n idx++;\n }\n else\n {\n int lb=upper_bound(dp.begin(),dp.end(),x)-dp.begin();\n dp[lb]=x;\n }\n }\n return n-idx;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "44170"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int count_a=0;\n int count_b=0;\n int count=0;\n vector<int>arr1;\n vector<int>arr2;\n int min=0;\n int i=0;\n int j=s.size()-1;\n while(i<s.size()&&s[i]=='a')i++;\n while(j>0&&s[j]=='b')j--;\n while(i<=j){\n \n while(i<=j&&s[i]=='b'){\n count_b++;\n i++;\n }\n arr1.push_back(count_b);\n while(i<=j&&s[i]=='a'){\n count_a++;\n i++;\n }\n arr2.push_back(count_a);\n count_a=0;\n }\n int sum=0;\n if(arr1.size()>0&&arr2.size()>0){\n for(i=arr2.size()-1;i>=0;i--){\n sum+=arr2[i];\n arr2[i]=sum;\n }\n min=arr2[0];\n j=1;\n for(int i=0;i<arr1.size();i++){\n if(j<arr2.size()&&min>arr1[i]+arr2[j])min=arr1[i]+arr2[j];\n j++;\n }\n if(min>arr1[arr1.size()-1])min=arr1[arr1.size()-1];\n }\n\n \n return min;\n }\n};",
"memory": "44170"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.length(), i = 0, j = n-1;\n while(i<n && s[i] == 'a') i++;\n while(j>=0 && s[j] == 'b') j--;\n vector<vector<int>> arr(2);\n int count = 0, prev = '.', counta = 0;\n for(int k = i; k<=j; k++){\n if(s[k] != prev){\n if(count != 0) arr[prev-'a'].push_back(count);\n if(prev == 'a') counta += count;\n count = 1;\n prev = s[k];\n }else count++;\n }\n if(count != 0) arr[prev-'a'].push_back(count);\n if(prev == 'a') counta += count;\n int totala = 0, totalb = 0, ans = counta;\n for(int k = 0; k<arr[0].size(); k++){\n totala += arr[0][k];\n totalb += arr[1][k];\n ans = min(ans, totalb + counta - totala);\n }\n return ans;\n }\n};",
"memory": "44590"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int count_a=0;\n int count_b=0;\n int count=0;\n vector<int>arr1;\n vector<int>arr2;\n int min=0;\n int i=0;\n int j=s.size()-1;\n while(i<s.size()&&s[i]=='a')i++;\n while(j>0&&s[j]=='b')j--;\n while(i<=j){\n \n while(i<=j&&s[i]=='b'){\n count_b++;\n i++;\n }\n arr1.push_back(count_b);\n while(i<=j&&s[i]=='a'){\n count_a++;\n i++;\n }\n arr2.push_back(count_a);\n count_a=0;\n }\n int sum=0;\n if(arr1.size()>0&&arr2.size()>0){\n for(i=arr2.size()-1;i>=0;i--){\n sum+=arr2[i];\n arr2[i]=sum;\n }\n min=arr2[0];\n j=1;\n for(int i=0;i<arr1.size();i++){\n if(j<arr2.size()&&min>arr1[i]+arr2[j])min=arr1[i]+arr2[j];\n j++;\n }\n if(min>arr1[arr1.size()-1])min=arr1[arr1.size()-1];\n }\n\n \n return min;\n }\n};",
"memory": "44590"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n;\n vector<int> dp;\n\n int f(int i,int &cntA,string& s){\n if(i==n) return 0;\n if(dp[i]!=-1) return dp[i];\n int ans=f(i+1,cntA,s);\n\n if(s[i]=='a') cntA++;\n else ans=min(ans+1,cntA);\n\n return dp[i]=ans;\n }\n int minimumDeletions(string s) {\n n=s.size();\n dp.assign(n,-1);\n int cntA=0;\n return f(0,cntA,s);\n \n }\n};\n\nauto init = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "45010"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<int> cnt;\n int dp[100000];\n int rec(int ind,string &s){\n if(ind==s.size()) return 0;\n if(dp[ind]!=-1) return dp[ind];\n if(s[ind]=='a')\n return dp[ind]=rec(ind+1,s);\n return dp[ind]=min(1+rec(ind+1,s),cnt[ind]);\n }\npublic:\n int minimumDeletions(string s) {\n memset(dp,-1,sizeof dp);\n int n=s.size();\n cnt.resize(n,0);\n for(int i=n-2;i>=0;i--){\n cnt[i]=cnt[i+1]+(s[i+1]=='a');\n }\n return rec(0,s);\n }\n};",
"memory": "45010"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int minDeletions;\n \n void updateChar(string s, vector<int>& charCount,int start, int end, int increment, char ch, bool secondPass){\n int currCount = 0;\n for(int i = start; i != end; i += increment){\n if(s[i - increment] == ch) currCount++;\n charCount[i] += currCount;\n if(secondPass) minDeletions = min(minDeletions, charCount[i]);\n }\n }\npublic:\n int minimumDeletions(string s) {\n if(s.length() < 2) return 0;\n vector<int> charCount(s.length());\n minDeletions = INT_MAX;\n updateChar(s, charCount, 1, s.length(), 1, 'b', false);\n updateChar(s, charCount, s.length() - 2, -1, -1, 'a', true);\n minDeletions = min(minDeletions, charCount[s.length() - 1]);\n return minDeletions;\n }\n};",
"memory": "45430"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int minDeletions;\n \n void updateChar(string s, vector<int>& charCount,int start, int end, int increment, char ch, bool secondPass){\n int currCount = 0;\n for(int i = start; i != end; i += increment){\n if(s[i - increment] == ch) currCount++;\n charCount[i] += currCount;\n if(secondPass) minDeletions = min(minDeletions, charCount[i]);\n }\n }\npublic:\n int minimumDeletions(string s) {\n if(s.length() < 2) return 0;\n vector<int> charCount(s.length());\n minDeletions = INT_MAX;\n updateChar(s, charCount, 1, s.length(), 1, 'b', false);\n updateChar(s, charCount, s.length() - 2, -1, -1, 'a', true);\n minDeletions = min(minDeletions, charCount[s.length() - 1]);\n return minDeletions;\n }\n};",
"memory": "45430"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int minDeletions;\n void updateChar(string s, vector<int>& charCount,int start, int end, int increment, char ch, bool secondPass){\n int currCount = 0;\n for(int i = start; i != end; i += increment){\n if(s[i - increment] == ch) currCount++;\n charCount[i] += currCount;\n if(secondPass) minDeletions = min(minDeletions, charCount[i]);\n }\n if(secondPass) minDeletions = min(minDeletions, charCount[s.length() - 1]);\n }\npublic:\n int minimumDeletions(string s) {\n if(s.length() < 2) return 0;\n vector<int> charCount(s.length());\n minDeletions = INT_MAX;\n updateChar(s, charCount, 1, s.length(), 1, 'b', false);\n updateChar(s, charCount, s.length() - 2, -1, -1, 'a', true);\n return minDeletions;\n }\n};",
"memory": "45850"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int minDeletions;\n \n void updateChar(string s, vector<int>& charCount,int start, int end, int increment, char ch, bool secondPass){\n int currCount = 0;\n for(int i = start; i != end; i += increment){\n if(s[i - increment] == ch) currCount++;\n charCount[i] += currCount;\n if(secondPass) minDeletions = min(minDeletions, charCount[i]);\n }\n }\npublic:\n int minimumDeletions(string s) {\n if(s.length() < 2) return 0;\n vector<int> charCount(s.length());\n minDeletions = INT_MAX;\n updateChar(s, charCount, 1, s.length(), 1, 'b', false);\n updateChar(s, charCount, s.length() - 2, -1, -1, 'a', true);\n minDeletions = min(minDeletions, charCount[s.length() - 1]);\n return minDeletions;\n }\n};",
"memory": "45850"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n\n static int minimumDeletions(const string &s) {\n const int n = s.size();\n struct val {\n int end_a = 0, end_b = 0;\n };\n vector<val> dp(n + 1);\n for (int i = 1; i <= n; ++i)\n if (s[i - 1] == 'a') {\n dp[i].end_a = dp[i - 1].end_a;\n dp[i].end_b = dp[i - 1].end_b + 1;\n } else {\n dp[i].end_a = dp[i - 1].end_a + 1;\n dp[i].end_b = min(dp[i - 1].end_a, dp[i - 1].end_b);\n }\n return min(dp[n].end_a, dp[n].end_b);\n }\n};",
"memory": "48790"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint minimumDeletions(const string &s) {\n int last_a = -1;\n int first_b = s.size();\n vector<pair<int, int>> vp(s.size());\n\n for (int i = 0; i < s.size(); i++)\n if (s[i] == 'a')\n last_a = i;\n else {\n first_b = min(first_b, i);\n }\n\n int counta = 0, countb = 0;\n for (int i = first_b; i <= last_a && i < s.size(); i++) {\n if (s[i] == 'b')\n countb++;\n vp[i].first = countb;\n }\n\n for (int i = min((int)s.size() - 1, last_a); i >= first_b; i--) {\n if (s[i] == 'a')\n counta++;\n vp[i].second = counta;\n }\n\n int mint = INT_MAX;\n for (int i = first_b; i <= last_a && i < s.size(); i++) {\n mint = min(vp[i].first + vp[i].second, mint);\n }\n\n if(mint >= s.size()) return 0;\n return mint - 1;\n}\n};",
"memory": "48790"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dfs(int ind,vector<pair<int,int>> &nums,vector<int> &dp)\n {\n if(ind==nums.size())\n return 0;\n if(dp[ind]!=-1)\n return dp[ind];\n return dp[ind] = min(nums[ind].second,nums[ind].first+dfs(ind+1,nums,dp));\n }\n int minimumDeletions(string s) \n {\n int n=s.length(),cnt=0,tot_a=-1;\n for(char c:s)\n {\n if(c=='b' && tot_a==-1)\n tot_a=0;\n if(c=='a' && tot_a!=-1)\n tot_a++;\n }\n // cout<<tot_a<<endl;\n vector<pair<int,int>> nums;\n for(int i=0;i<n;i++)\n {\n if(s[i]=='b')\n {\n int j=i,cnt=0,cnt1=0;\n while(j<n && s[j]=='b')\n cnt++,j++;\n while(j<n && s[j]=='a')\n cnt1++,j++;\n nums.push_back({cnt,tot_a});\n tot_a-=cnt1;\n i=j-1;\n }\n }\n // for(auto it:nums)\n // cout<<it.first<<\" \"<<it.second<<endl;\n int m=nums.size();\n vector<int> dp(m,-1);\n return dfs(0,nums,dp); \n }\n};",
"memory": "49210"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string& s) {\n const int n=s.size();\n vector<int> B_pre(n), A_suf(n);//0-based prefix sums & suffix sums\n B_pre[0]=s[0]=='b';\n A_suf[0]=count(s.begin(), s.end(), 'a');// Count 'a' in s\n for(int i=1; i<n; i++){\n B_pre[i]=B_pre[i-1]+(s[i]=='b');\n A_suf[i]=A_suf[i-1]-(s[i-1]=='a');\n }\n int cnt=n;\n for(int i=0; i<n; i++){\n cnt=min(cnt, B_pre[i]+A_suf[i]-1);\n }\n return cnt;\n }\n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "49210"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string& s) {\n const int n=s.size();\n vector<int> B_pre(n), A_suf(n);//0-based prefix sums & suffix sums\n B_pre[0]=s[0]=='b';\n A_suf[0]=count(s.begin(), s.end(), 'a');// Count 'a' in s\n for(int i=1; i<n; i++){\n B_pre[i]=B_pre[i-1]+(s[i]=='b');\n A_suf[i]=A_suf[i-1]-(s[i-1]=='a');\n }\n int cnt=n;\n for(int i=0; i<n; i++){\n cnt=min(cnt, B_pre[i]+A_suf[i]-1);\n }\n return cnt;\n }\n};\n\n\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "49630"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int other(string s,stack<int>&st){\n int count=0;\n for(int i=0;i<s.length();i++){\n if(st.empty()){\n st.push(s[i]);\n }\n else if(st.top()=='a' && (s[i]=='b' || s[i]=='a')){\n st.push(s[i]);\n }\n else if(st.top()=='b' && s[i]=='b'){\n st.push(s[i]);\n }\n else if(!st.empty() && (st.top()=='b' && s[i]=='a')){\n st.pop();\n // st.push(s[i]);\n count++;\n }\n }\n return count;\n }\n int mindeletion(string s,stack<int>& st){\n int count=0;\n for(int i=0;i<s.length();i++){\n if(st.empty()){\n st.push(s[i]);\n }\n else if(st.top()=='a' && (s[i]=='b' || s[i]=='a')){\n st.push(s[i]);\n }\n else if(st.top()=='b' && (s[i]=='b')){\n st.push(s[i]);\n }\n else if(st.top()=='b' && s[i]=='a'){\n count++;\n }\n }\n return count;\n }\n int minimumDeletions(string s) {\n stack<int> st;\n stack<int> ct;\n int a=mindeletion(s,st);\n int b=other(s,ct);\n return min(a,b);\n }\n};",
"memory": "49630"
} |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i < j</code> and <code>s[i] = 'b'</code> and <code>s[j]= 'a'</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbab"
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 ("aa<u>b</u>abb<u>a</u>b" -> "aaabbb"), or
Delete the characters at 0-indexed positions 3 and 6 ("aab<u>a</u>bb<u>a</u>b" -> "aabbbb").
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "bbaaaaabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is <code>'a'</code> or <code>'b'</code>ββ.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int other(string s,stack<int>&st){\n int count=0;\n for(int i=0;i<s.length();i++){\n if(st.empty()){\n st.push(s[i]);\n }\n else if(st.top()=='a' && (s[i]=='b' || s[i]=='a')){\n st.push(s[i]);\n }\n else if(st.top()=='b' && s[i]=='b'){\n st.push(s[i]);\n }\n else if(!st.empty() && (st.top()=='b' && s[i]=='a')){\n st.pop();\n count++;\n }\n }\n return count;\n }\n int mindeletion(string s,stack<int>& st){\n int count=0;\n for(int i=0;i<s.length();i++){\n if(st.empty()){\n st.push(s[i]);\n }\n else if(st.top()=='a' && (s[i]=='b' || s[i]=='a')){\n st.push(s[i]);\n }\n else if(st.top()=='b' && (s[i]=='b')){\n st.push(s[i]);\n }\n else if(st.top()=='b' && s[i]=='a'){\n count++;\n }\n }\n return count;\n }\n int minimumDeletions(string s) {\n stack<int> st;\n stack<int> ct;\n int a=mindeletion(s,st);\n int b=other(s,ct);\n return min(a,b);\n }\n};",
"memory": "50050"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.