id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n=nums.size();\n long long sum=nums[0];\n k--;\n multiset<int> l,r;\n for(int i=1;i<=min(n,dist+1);i++){\n sum+=nums[i];\n l.insert(nums[i]);\n } // First Window Done.\n while(l.size()>k){\n r.insert(*l.rbegin());\n sum-=*l.rbegin();\n l.erase(l.find(*l.rbegin()));\n }\n long long ans=sum;\n for(int i=dist+2;i<n;i++){\n int delEl=nums[i-dist-1];\n if(l.find(delEl)!=l.end()){\n sum-=delEl;\n l.erase(l.find(delEl));\n }\n else{\n r.erase(r.find(delEl));\n }\n if(nums[i]<*l.rbegin()){\n sum+=nums[i];\n l.insert(nums[i]);\n }\n else {\n r.insert(nums[i]);\n }\n while(l.size()<k){\n sum+=*r.begin();\n l.insert(*r.begin());\n r.erase(r.find(*r.begin()));\n }\n while (l.size() > k) {\n sum -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n ans=min(ans,sum);\n\n }\n return ans; \n }\n};\n\n",
"memory": "156500"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 0 | {
"code": "#define ll long long int\nclass Solution {\n multiset<int> l, r;\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n long long curr = nums[0];\n k--;\n for (int i = 1; i <= dist + 1; i++)\n {\n curr += nums[i];\n l.insert(nums[i]);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n curr -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n ll ans = curr;\n\n for(int i = dist+2; i < n; i++){\n //deleting the element\n if(l.find(nums[i-dist-1]) != l.end()){\n curr -= nums[i-dist-1];\n l.erase(l.find(nums[i-dist-1]));\n }\n else{\n r.erase(r.find(nums[i-dist-1]));\n }\n\n //adding the element\n if(nums[i] < *l.rbegin()){\n curr += nums[i];\n l.insert(nums[i]);\n }\n else{\n r.insert(nums[i]);\n }\n\n while(l.size() < k){\n curr += *r.begin();\n l.insert(*r.begin());\n r.erase(r.find(*r.begin()));\n }\n while(l.size() > k){\n curr -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n\n ans = min(ans, curr);\n }\n return ans;\n }\n};",
"memory": "156700"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 0 | {
"code": "#define ll long long int\nclass Solution {\nprivate:\nmultiset<int> l, r;\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n ll n = nums.size();\n // Make the first Window for which the elementsthere exists the nums[0] (ofcourse) and the lowest k-1 elemnts of the window of the size dist+1\n k--;\n ll curr = nums[0];\n for(int i=1; i<=dist+1; i++){\n curr += nums[i];\n l.insert(nums[i]);\n }\n // Now my curr can only have k elements, this means apart from the first element(k-- hs been done for it), there must be only k elements in my set l\n while(l.size() > k){\n curr -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n\n ll ans = curr;\n // Now I have ans for the first window stored in ans\n // Now my aim is to find for other windows,\n // right now my l multiset contians the k elements for the first window and the r is having the possible elements\n // which can come on need\n\n // Now check for other windows\n for(int i=dist+2; i<n; i++){\n // if you find the last element in l, this means in new window it should not be there thus delete it\n if(l.find(nums[i-dist-1]) != l.end()){\n curr -= nums[i-dist-1];\n l.erase(l.find(nums[i-dist-1]));\n }\n // else means it is in r, not in curr so no deletion but erase it from r as it is no longer a buffer\n else{\n r.erase(r.find(nums[i-dist-1]));\n }\n\n // Now update for the new index nums[i] which has come\n if(nums[i] < *l.rbegin()){\n // this means it has to be a part of l and curr\n curr += nums[i];\n l.insert(nums[i]);\n }\n // this means the newly added number is not good enough to be added in that new window so put it in buffer\n else{\n r.insert(nums[i]);\n }\n\n // Balance the l if it does not k elements any more\n while(l.size() < k){\n // if there are less elements then simply take the extra minimum element from r and put it in l while updating the curr \n l.insert(*r.begin());\n curr += *r.begin();\n r.erase(r.find(*r.begin()));\n }\n while(l.size() > k){\n // if there are more element in l then simplt take the maximum element from l which are extra and pur it in r while updating the curr\n r.insert(*l.rbegin());\n curr -= *l.rbegin();\n l.erase(l.find(*l.rbegin()));\n }\n\n ans = min(ans, curr); \n }\n return ans;\n }\n};",
"memory": "156800"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 0 | {
"code": "#define ll long long int\nclass Solution {\nprivate:\nmultiset<int> l, r;\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n ll n = nums.size();\n k--;\n ll curr = nums[0];\n for(int i=1; i<=dist+1; i++){\n curr += nums[i];\n l.insert(nums[i]);\n }\n while(l.size() > k){\n curr -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n ll ans = curr;\n for(int i=dist+2; i<n; i++){\n if(l.find(nums[i-dist-1]) != l.end()){\n curr -= nums[i-dist-1];\n l.erase(l.find(nums[i-dist-1]));\n }\n else{\n r.erase(r.find(nums[i-dist-1]));\n }\n if(nums[i] < *l.rbegin()){\n curr += nums[i];\n l.insert(nums[i]);\n }\n else{\n r.insert(nums[i]);\n }\n while(l.size() < k){\n \n l.insert(*r.begin());\n curr += *r.begin();\n r.erase(r.find(*r.begin()));\n }\n while(l.size() > k){\n r.insert(*l.rbegin());\n curr -= *l.rbegin();\n l.erase(l.find(*l.rbegin()));\n }\n\n ans = min(ans, curr); \n }\n return ans;\n }\n};",
"memory": "156900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n long window_sum = 0;\n std::multiset<int> selected;\n std::multiset<int> candidates;\n auto balance = [&]() -> void\n {\n while (static_cast<int>(selected.size()) < k - 1)\n {\n const int min_candidate = *candidates.begin();\n window_sum += min_candidate;\n selected.insert(min_candidate);\n candidates.erase(candidates.find(min_candidate));\n }\n while (static_cast<int>(selected.size()) > k - 1)\n {\n const int max_selected = *selected.rbegin();\n window_sum -= max_selected;\n selected.erase(selected.find(max_selected));\n candidates.insert(max_selected);\n }\n };\n \n for (int i = 1; i <= dist + 1; ++i)\n {\n window_sum += nums[i];\n selected.insert(nums[i]);\n }\n balance();\n long min_window_sum = window_sum;\n for (int i = dist + 2, n = static_cast<int>(nums.size()); i < n; ++i)\n {\n const int out_of_scope = nums[i - dist - 1];\n if (selected.find(out_of_scope) != selected.end())\n {\n window_sum -= out_of_scope;\n selected.erase(selected.find(out_of_scope));\n }\n else candidates.erase(candidates.find(out_of_scope));\n if (nums[i] < *selected.rbegin())\n {\n window_sum += nums[i];\n selected.insert(nums[i]);\n }\n else candidates.insert(nums[i]);\n balance();\n min_window_sum = std::min(min_window_sum, window_sum);\n }\n return nums[0] + min_window_sum;\n }\n};",
"memory": "157000"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "#define ll long long int\nclass Solution {\n multiset<ll> l, r;\npublic:\n ll minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n ll cur = nums[0];\n for (int i = 1; i <= dist + 1; i++) cur += nums[i], l.insert(nums[i]);\n while (l.size() > k) {\n cur -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n\n ll ans = cur;\n for (int i = dist + 2; i < n; i++) {\n if (l.find(nums[i - dist - 1]) != l.end()) {\n cur -= nums[i - dist - 1];\n l.erase(l.find(nums[i - dist - 1]));\n } else {\n r.erase(r.find(nums[i - dist - 1]));\n }\n if (nums[i] < *l.rbegin()) {\n cur += nums[i];\n l.insert(nums[i]);\n } else {\n r.insert(nums[i]);\n }\n while (l.size() < k) {\n cur += *r.begin();\n l.insert(*r.begin());\n r.erase(r.find(*r.begin()));\n }\n while (l.size() > k) {\n cur -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n ans = min(ans, cur);\n }\n return ans;\n }\n};\n",
"memory": "157100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "class Solution {\n multiset<long long> l, r;\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n long long cur = nums[0];\n for (int i = 1; i <= dist + 1; i++) cur += nums[i], l.insert(nums[i]);\n while (l.size() > k) {\n cur -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n long long ans = cur;\n\n for (int i = dist + 2; i < n; i++) {\n // Erasing an element from the set\n if (l.find(nums[i - dist - 1]) != l.end()) {\n cur -= nums[i - dist - 1];\n l.erase(l.find(nums[i - dist - 1]));\n } else {\n r.erase(r.find(nums[i - dist - 1]));\n }\n // Adding an element to the set\n if (nums[i] < *l.rbegin()) {\n cur += nums[i];\n l.insert(nums[i]);\n } else {\n r.insert(nums[i]);\n }\n // Balancing the set such that the first set has (k - 1) elements \n while (l.size() < k) {\n cur += *r.begin();\n l.insert(*r.begin());\n r.erase(r.find(*r.begin()));\n }\n while (l.size() > k) {\n cur -= *l.rbegin();\n r.insert(*l.rbegin());\n l.erase(l.find(*l.rbegin()));\n }\n // Finally, update the minimum\n ans = min(ans, cur);\n }\n return ans;\n }\n};",
"memory": "157200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "//sliding window\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n multiset<long long> st1, st2;\n long long curr = nums[0]; // we need to include thios always\n k--;\n\n // sum of all the elements from index 1 to dist + 1\n for(int i = 1; i <= dist + 1; i++) {\n curr += nums[i];\n st1.insert(nums[i]);\n }\n\n //now removing the larger numbers from last of the set to get k elements only\n while(st1.size() > k) {\n curr -= *st1.rbegin();\n st2.insert(*st1.rbegin());\n st1.erase(st1.find(*st1.rbegin()));\n }\n\n long long ans = curr;\n //now we will shift our window of size 'dist' to one step forward in each step, so we need to delete the nums[i-dist-1] from our set\n for(int i = dist + 2; i < n; i++) {\n //removing the starting element of the prev. window\n if(st1.find(nums[i-dist-1]) != st1.end()) {\n curr -= nums[i-dist-1];\n st1.erase(st1.find(nums[i-dist-1]));\n }\n else {\n st2.erase(st2.find(nums[i-dist-1]));\n }\n\n //adding current element into our set\n if(nums[i] < *st1.rbegin()) {\n curr += nums[i];\n st1.insert(nums[i]);\n }\n else {\n st2.insert(nums[i]);\n }\n //we want our set 1 i.e. st1 to have (k-1) elements exactly\n while(st1.size() < k) { //if size is large than remove large elements from set 1 and insert them into set 2\n curr += *st2.begin();\n st1.insert(*st2.begin());\n st2.erase(st2.find(*st2.begin()));\n }\n while(st1.size() > k) { //if size is large than remove large elements from set 1 and insert them into set 2\n curr -= *st1.rbegin();\n st2.insert(*st1.rbegin());\n st1.erase(st1.find(*st1.rbegin()));\n }\n ans = min(ans, curr);\n }\n return ans;\n }\n};\n// Hint 1\n// For each i > 0, try each nums[i] as the first element of the second subarray. We need to find the sum of k - 2 smallest values in the index range [i + 1, min(i + dist, n - 1)].\n// Hint 2\n// Typically, we use a max heap to maintain the top k - 2 smallest values dynamically. Here we also have a sliding window, which is the index range [i + 1, min(i + dist, n - 1)]. We can use another min heap to put unselected values for future use.\n// Hint 3\n// Update the two heaps when iteration over i. Ordered/Tree sets are also a good choice since we have to delete elements.\n// Hint 4\n// If the max heap’s size is less than k - 2, use the min heap’s value to fill it. If the maximum value in the max heap is larger than the smallest value in the min heap, swap them in the two heaps.\n// Intuition\n// You need to solve this question using SLIDING WINDOW. I guess till now you must have figured it out by yourself.\n\n// Lets discuss the optimzed solution in below section !!\n\n// Approach\n// What we need in each window ?\n// We need (k-1) minimum elements in the window because nums[0] is our 1st element which we always need to consider.\n// Now, for the elements from index 1 to dist+1, we will consider our 1st window and insert the elements in one multiset.\n// Now, we need only (k-1) elements, so we will insert rest of the larger elements into second set.\n// Till now, this must be clear !!\n// Now, moving on.\n// What for the rest of the windows which are starting from index 2,3,etc.\n// We will move our window to one step forward and remove the first element of the previous window, so that size of our window remains 'dist' only acc. to conditon given in question.\n// So, for this we will first find out our nums[i-dist-1] element is present where? either in set1 or set2. After finding we will remove it.\n// Now, we need to insert our current element into our set, so we will insert in one of our two sets, depending on it's value. If it's value is less than largest element of our first set then we will insert it into first set otherwise we will insert it into second set.\n// MOST IMP THING\n// we need to keep the size of our first set equal to (k-1) because we need only minimum (k-1) elements, so for this we will either insert elements or remove elements from our set1.\n// Finally, for each window, we will compare curr sum of elements of set1 with a variable 'ans' to get the final answer !!!!!\n// Complexity\n// Time complexity: O(nlogn)\n// Space complexity: O(n)",
"memory": "157300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n \n multiset<int>m1,m2;\n long long i,n=nums.size(),cnt=nums[0],ans=0;\n for(i=1;i<=dist+1&&i<n;i++)\n {\n m1.insert(nums[i]);\n cnt+=nums[i];\n }\n k--;\n while(m1.size()>k)\n {\n int temp = *m1.rbegin();\n m1.erase(m1.find(temp));\n cnt -= temp;\n m2.insert(temp);\n }\n ans = cnt;\n\n for(i=2;i+dist<n;i++)\n {\n int pre = nums[i-1];\n int cur = nums[i+dist];\n if(m1.find(pre)!=m1.end()) \n {\n m1.erase(m1.find(pre));\n cnt -= pre;\n }\n else m2.erase(m2.find(pre));\n\n if(cur< *m1.rbegin())\n {\n m1.insert(cur);\n cnt+=cur;\n }\n else{\n m2.insert(cur);\n }\n\n while(m1.size()<k)\n {\n int temp = *m2.begin();\n cnt+=temp;\n m2.erase(m2.find(temp));\n m1.insert(temp);\n cout<<i<<\" \"<<m1.size();\n }\n\n while(m1.size()>k)\n {\n int temp = *m1.rbegin();\n m1.erase(m1.find(temp));\n cnt -= temp;\n m2.insert(temp);\n \n }\n \n ans = min(ans,cnt);\n }\n\n return ans;\n }\n};",
"memory": "157600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "class Solution\n{\n multiset<int> l, r;\n\npublic:\n long long minimumCost(vector<int> &nums, int k, int dist)\n {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n int n = nums.size();\n long long cnt = nums[0];\n k--;\n\n // Add the first 'dist + 1' elements to the left multiset\n for (int i = 1; i <= dist + 1; i++)\n {\n cnt += nums[i];\n l.insert(nums[i]);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n cnt -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n long long ans = cnt;\n\n // Iterate through the array from index 'dist + 2'\n for (int i = dist + 2; i < n; i++)\n {\n int pre = nums[i - dist - 1];\n\n // Remove the element at index 'i - dist - 1' from the appropriate multiset\n if (l.find(pre) != l.end())\n {\n cnt -= pre;\n l.erase(l.find(pre));\n }\n else\n {\n r.erase(r.find(pre));\n }\n\n // Add the element at index 'i' to the appropriate multiset\n int cur = nums[i];\n if (cur < *l.rbegin())\n {\n cnt += cur;\n l.insert(cur);\n }\n else\n {\n r.insert(cur);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() < k)\n {\n int temp = *r.begin();\n cnt += temp;\n l.insert(temp);\n r.erase(r.find(temp));\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n cnt -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n // Update the answer as the minimum between the current answer and 'cnt'\n ans = min(ans, cnt);\n }\n\n return ans;\n }\n};",
"memory": "157800"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "class Solution\n{\n multiset<int> l, r;\n\npublic:\n long long minimumCost(vector<int> &nums, int k, int dist)\n {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n int n = nums.size();\n long long cnt = nums[0];\n k--;\n\n // Add the first 'dist + 1' elements to the left multiset\n for (int i = 1; i <= dist + 1; i++)\n {\n cnt += nums[i];\n l.insert(nums[i]);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n cnt -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n long long ans = cnt;\n\n // Iterate through the array from index 'dist + 2'\n for (int i = dist + 2; i < n; i++)\n {\n int pre = nums[i - dist - 1];\n\n // Remove the element at index 'i - dist - 1' from the appropriate multiset\n if (l.find(pre) != l.end())\n {\n cnt -= pre;\n l.erase(l.find(pre));\n }\n else\n {\n r.erase(r.find(pre));\n }\n\n // Add the element at index 'i' to the appropriate multiset\n int cur = nums[i];\n if (cur < *l.rbegin())\n {\n cnt += cur;\n l.insert(cur);\n }\n else\n {\n r.insert(cur);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() < k)\n {\n int temp = *r.begin();\n cnt += temp;\n l.insert(temp);\n r.erase(r.find(temp));\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n cnt -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n // Update the answer as the minimum between the current answer and 'cnt'\n ans = min(ans, cnt);\n }\n\n return ans;\n }\n};",
"memory": "157900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 1 | {
"code": "class Solution\n{\n multiset<int> l, r;\n\npublic:\n long long minimumCost(vector<int> &nums, int k, int dist)\n {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n int n = nums.size();\n long long cnt = nums[0];\n k--;\n\n // Add the first 'dist + 1' elements to the left multiset\n for (int i = 1; i <= dist + 1; i++)\n {\n cnt += nums[i];\n l.insert(nums[i]);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n cnt -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n long long ans = cnt;\n\n // Iterate through the array from index 'dist + 2'\n for (int i = dist + 2; i < n; i++)\n {\n int pre = nums[i - dist - 1];\n\n // Remove the element at index 'i - dist - 1' from the appropriate multiset\n if (l.find(pre) != l.end())\n {\n cnt -= pre;\n l.erase(l.find(pre));\n }\n else\n {\n r.erase(r.find(pre));\n }\n\n // Add the element at index 'i' to the appropriate multiset\n int cur = nums[i];\n if (cur < *l.rbegin())\n {\n cnt += cur;\n l.insert(cur);\n }\n else\n {\n r.insert(cur);\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() < k)\n {\n int temp = *r.begin();\n cnt += temp;\n l.insert(temp);\n r.erase(r.find(temp));\n }\n\n // Balance the left multiset to have 'k' elements\n while (l.size() > k)\n {\n int temp = *l.rbegin();\n cnt -= temp;\n r.insert(temp);\n l.erase(l.find(temp));\n }\n\n // Update the answer as the minimum between the current answer and 'cnt'\n ans = min(ans, cnt);\n }\n\n return ans;\n }\n};",
"memory": "157900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n // return byPQ(nums, k, dist);\n\n return byMultiset(nums, k, dist);\n }\n\n long long byPQ(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n\n long long sum = accumulate(nums.begin(), nums.begin()+dist+2, 0LL);\n\n priority_queue<int> left(nums.begin()+1, nums.begin()+dist+2);\n priority_queue<int,vector<int>,greater<int>> right;\n\n auto left2right = [&](){\n sum -= left.top();\n right.push(left.top());\n left.pop();\n };\n\n auto right2left = [&](){\n sum += right.top();\n left.push(right.top());\n right.pop();\n };\n\n while(left.size()>k){\n left2right();\n }\n\n unordered_map<int,int> delCnt;\n\n long long ans = sum;\n\n for(int i=dist+2; i<n; ++i){\n int del = nums[i-dist-1];\n int add = nums[i];\n\n int balance = 0;\n\n delCnt[del]++;\n\n if(del<=left.top()){\n sum -= del;\n balance--;\n } else {\n balance++;\n }\n\n if (add<=left.top()){\n sum += add;\n left.push(add);\n balance++;\n } else {\n right.push(add);\n balance--;\n }\n\n if(balance>0){\n left2right();\n } else if (balance<0){\n right2left();\n }\n\n while(!left.empty() && delCnt[left.top()]){\n delCnt[left.top()]--;\n left.pop();\n }\n\n while(!right.empty() && delCnt[right.top()]){\n delCnt[right.top()]--;\n right.pop();\n }\n\n ans = min(ans, sum);\n }\n\n return ans;\n }\n\n long long byMultiset(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n long long sum = accumulate(nums.begin(),nums.begin()+dist+2, 0LL);\n\n multiset<int> left(nums.begin()+1, nums.begin()+dist+2);\n multiset<int> right;\n\n auto left2right = [&](){\n int x = *left.rbegin();\n sum -= x;\n right.insert(x);\n left.erase(left.find(x));\n };\n\n auto right2left = [&](){\n int x = *right.begin();\n sum += x;\n left.insert(x);\n right.erase(right.begin());\n };\n\n while(left.size()>k){\n left2right();\n }\n\n long long ans = sum;\n\n for(int i=dist+2; i<n; ++i){\n int out = nums[i-dist-1];\n int balance = 0;\n\n auto it = left.find(out);\n if (it!=left.end()){\n left.erase(it);\n sum -= out;\n balance--;\n } else {\n right.erase(right.find(out));\n // balance++;\n }\n\n auto in = nums[i];\n if (in <= *left.rbegin()){\n sum += in;\n balance++;\n left.insert(in);\n } else {\n // balance--;\n right.insert(in);\n }\n\n if (balance>0){\n left2right();\n } else if (balance<0){\n right2left();\n }\n\n ans = min(ans, sum);\n }\n\n return ans;\n }\n};",
"memory": "158200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "typedef long long ll;\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n // vector<ll> temp;\n // for(int i = 1;i <= 1 + dist;i++)\n // {\n // temp.push_back(nums[i]);\n // }\n // sort(temp.rbegin(),temp.rend());\n // ll sumo = 0;\n // for(int i = 3;i < temp.size();i++)\n // {\n // sumo = temp[i];\n // cout<<sumo<<\" \";\n // }\n // cout<<endl;\n\n // vector<ll> temp1;\n // for(int i = 2;i <= 2 + dist;i++)\n // {\n // temp1.push_back(nums[i]);\n // }\n // sort(temp1.rbegin(),temp1.rend());\n // sumo = 0;\n // for(int i = 3;i < temp1.size();i++)\n // {\n // sumo = temp1[i];\n // cout<<sumo<<\" \";\n // }\n // cout<<endl;\n\n // vector<ll> temp2;\n // for(int i = 3;i <= 3 + dist;i++)\n // {\n // // cout<<\"i is \"<<i<<endl;\n // temp2.push_back(nums[i]);\n // }\n // sort(temp2.rbegin(),temp2.rend());\n // sumo = 0;\n // for(int i = 3;i < temp2.size();i++)\n // {\n // sumo = temp2[i];\n // cout<<sumo<<\" \";\n // }\n // cout<<endl;\n\n\n\n\n\n\n // cout<<\"ss is \"<<nums.size()<<endl;\n ll res = nums[0];\n ll sumi = 0;\n multiset<ll,greater<ll>> s;\n int secondWindow = (dist+1) - (k-1);\n multiset<ll> s2;\n\n for(int i = 1;i < dist + 2 && i < nums.size();i++)\n {\n sumi += nums[i];\n s.insert(nums[i]);\n if(s.size() > (k-1))\n {\n auto it = s.begin();\n sumi -= (*it);\n s2.insert((*it));\n s.erase(it);\n }\n // s2.insert(nums[i]);\n // if(s2.size() > secondWindow)\n // {\n // auto it = s2.begin();\n // sumi += (*it);\n // s.insert((*it));\n // s2.erase(it);\n // }\n // sumi += nums[i];\n // // cout<<\"first is \"<<sumi<<endl;\n // s.insert(nums[i]);\n\n // while(s.size() > k-1)\n // {\n // // auto it = s.end();\n // // it--;\n // auto it = s.begin();\n \n // sumi -= (*it);\n // // cout<<\"Now sum is \"<<sumi<<\" \"<<(*it)<<endl;\n // s.erase(it);\n // }\n }\n // auto it1 = s.begin();\n // while(it1 != s.end())\n // {\n // cout<<(*it1)<<\" \";\n // it1++;\n // }\n // cout<<endl;\n ll mini = res + sumi;\n // cout<<\"miini is \"<<mini<<\" \"<<sumi<<endl;\n int i = 1;\n int j = dist+2;\n // cout<<\"size is \"<<nums.size()<<endl;\n while(j < nums.size())\n {\n // cout<<\"i and j is \"<<i<<\" \"<<j<<\" \"<<sumi<<endl;\n auto it = s.find(nums[i]);\n if(it != s.end())\n {\n // cout<<\"kill is \"<<(*it)<<endl;\n sumi -= (*it);\n s.erase(it);\n }\n else\n {\n auto it1 = s2.find(nums[i]);\n if(it1 != s2.end())\n s2.erase(it1);\n }\n sumi += nums[j];\n s.insert(nums[j]);\n if(s.size() > (k-1))\n {\n auto it1 = s.begin();\n sumi -= (*it1);\n s2.insert((*it1));\n s.erase(it1);\n }\n else if(s2.size() > 0)\n {\n auto it1 = s2.begin();\n auto it2 = s.begin();\n if((*it2) > (*it1))\n {\n sumi -= (*it2);\n sumi += (*it1);\n s.insert((*it1));\n s2.insert((*it2));\n s2.erase(it1);\n s.erase(it2);\n }\n }\n // sumi += nums[j];\n // cout<<\"add is \"<<nums[j]<<endl;\n // s.insert(nums[j]);\n\n // while(s.size() > k-1)\n // {\n // auto it = s.begin();\n // // it--;\n // cout<<\"kill is \"<<(*it)<<endl;\n // sumi -= (*it);\n // s.erase(it);\n // }\n if(res + sumi < mini)\n {\n mini = res + sumi;\n // cout<<\"mini is \"<<sumi<<\" \"<<mini<<\" \"<<i+1<<\" \"<<j<<endl;\n }\n \n // auto it1 = s.begin();\n // while(it1 != s.end())\n // {\n // cout<<(*it1)<<\" \";\n // it1++;\n // }\n // cout<<endl;\n// mini = min(mini,res+sumi);\n i++;\n j++;\n }\n return mini;\n }\n};",
"memory": "165300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n \n ll minimumCost(vector<int>& a, int k, int di) {\n multiset<ll>l,r;\n ll s=0;\n int n=a.size();\n for(int i=1;i<=min(n-1,di+1);i++){\n r.insert(a[i]);\n } \n for(int i=0;i<k-1;i++){\n int x=*(r.begin());\n r.erase(r.find(x));\n l.insert(x);\n s+=x;\n }\n ll ans=a[0]+s;\n for(int i=di+2;i<n;i++){\n int rm=a[i-di-1];\n int cur=a[i];\n if(l.find(rm)!=l.end()){\n l.erase(l.find(rm));\n s-=rm;\n }else{\n r.erase(r.find(rm));\n }\n auto la=l.end();la--;\n int w=*la;\n if(cur<w){\n l.insert(cur);\n s+=cur;\n }else{\n r.insert(cur);\n }\n while(l.size()>k-1){\n auto it=l.end();it--;\n int y=*it;\n s-=y;\n r.insert(y);\n l.erase(l.find(y)); \n }\n while(l.size()<k-1){\n s+=*(r.begin());\n l.insert(*(r.begin()));\n r.erase(r.begin());\n }\n ans=min(ans,a[0]+s);\n }\n return ans;\n }\n};",
"memory": "165400"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "//m0\n//more general template with balance().\n\n//dual multiset for top k.\n\n//multiset.count() --> TLE. 'cuz it actually counts.\n\n//O(n log dist)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n multiset<int> chosen, candidate;\n int chosen_k;//k-1\n long long window_sum = 0;\n\n inline void insert(const int& val){\n window_sum += val;\n chosen.insert(val);\n }\n inline void erase_chosen(const int& val){\n window_sum -= val;\n //erase(val) erases all instances\n chosen.erase(chosen.find(val));\n }\n inline void erase(const int& val){\n //multiset.count() --> TLE\n if(chosen.find(val) != chosen.end()){\n erase_chosen(val);\n }else{\n candidate.erase( candidate.find(val) );\n }\n }\n\n inline void balance(){\n while(chosen.size() > chosen_k){\n int val = *chosen.rbegin();\n\n erase_chosen(val);\n\n candidate.insert(val);\n }\n while(chosen.size() < chosen_k && candidate.size()){\n int val = *candidate.begin();\n\n candidate.erase(candidate.begin());\n\n insert(val);\n }\n\n //crucial swapping\n while(chosen.size() && candidate.size() && *chosen.rbegin() > *candidate.begin()){\n int cho = *chosen.rbegin();\n int can = *candidate.begin();\n\n erase_chosen(cho);\n candidate.erase(candidate.begin());\n\n insert(can);\n candidate.insert(cho);\n }\n }\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n chosen_k = k-1;\n\n //make the 1st semi-window\n for(int i=1;i<=1+dist - 1;i++){\n insert(nums[i]);\n }\n\n long long min_sum = LLONG_MAX;\n for(int i=1;i<n;i++){\n int j = i+dist;\n if(!(j<n)) break;\n\n insert(nums[j]);//update\n\n balance();\n min_sum = min(min_sum, window_sum);\n\n erase(nums[i]);//update\n }\n return min_sum + nums.front();\n }\n};",
"memory": "166200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "//m0\n//more general template with balance().\n\n//dual multiset for top k.\n\n//multiset.count() --> TLE. 'cuz it actually counts.\n\n//O(n log dist)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n multiset<int> chosen, candidate;\n int chosen_k;//k-1\n long long window_sum = 0;\n\n inline void insert(const int& val){\n window_sum += val;\n chosen.insert(val);\n }\n inline void erase_chosen(const int& val){\n window_sum -= val;\n //erase(val) erases all instances\n chosen.erase(chosen.find(val));\n }\n inline void erase(const int& val){\n //multiset.count() --> TLE\n if(chosen.find(val) != chosen.end()){\n erase_chosen(val);\n }else{\n candidate.erase( candidate.find(val) );\n }\n }\n\n inline void balance(){\n while(chosen.size() > chosen_k){\n int val = *chosen.rbegin();\n\n erase_chosen(val);\n\n candidate.insert(val);\n }\n while(chosen.size() < chosen_k && candidate.size()){\n int val = *candidate.begin();\n\n candidate.erase(candidate.begin());\n\n insert(val);\n }\n\n //crucial swapping\n while(chosen.size() && candidate.size() && *chosen.rbegin() > *candidate.begin()){\n int cho = *chosen.rbegin();\n int can = *candidate.begin();\n\n erase_chosen(cho);\n candidate.erase(candidate.begin());\n\n insert(can);\n candidate.insert(cho);\n }\n }\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n chosen_k = k-1;\n\n //make the 1st semi-window\n for(int i=1;i<=1+dist - 1;i++){\n insert(nums[i]);\n }\n\n long long min_sum = LLONG_MAX;\n for(int i=1;i<n;i++){\n int j = i+dist;\n if(!(j<n)) break;\n\n insert(nums[j]);//update\n\n balance();\n min_sum = min(min_sum, window_sum);\n\n erase(nums[i]);//update\n }\n return min_sum + nums.front();\n }\n};",
"memory": "166300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "//m0\n//more general template with balance().\n\n//dual multiset for top k.\n\n//multiset.count() --> TLE. 'cuz it actually counts.\n\n//O(n log dist)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n multiset<int> chosen, candidate;\n int chosen_k;//k-1\n long long window_sum = 0;\n\n inline void insert(const int& val){\n window_sum += val;\n chosen.insert(val);\n }\n inline void erase_chosen(const int& val){\n window_sum -= val;\n //erase(val) erases all instances\n chosen.erase(chosen.find(val));\n }\n inline void erase(const int& val){\n //multiset.count() --> TLE\n if(chosen.find(val) != chosen.end()){\n erase_chosen(val);\n }else{\n candidate.erase( candidate.find(val) );\n }\n }\n\n inline void balance(){\n while(chosen.size() > chosen_k){\n int val = *chosen.rbegin();\n\n erase_chosen(val);\n\n candidate.insert(val);\n }\n while(chosen.size() < chosen_k && candidate.size()){\n int val = *candidate.begin();\n\n candidate.erase(candidate.begin());\n\n insert(val);\n }\n\n //crucial swapping\n while(chosen.size() && candidate.size() && *chosen.rbegin() > *candidate.begin()){\n int cho = *chosen.rbegin();\n int can = *candidate.begin();\n\n erase_chosen(cho);\n candidate.erase(candidate.begin());\n\n insert(can);\n candidate.insert(cho);\n }\n }\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n chosen_k = k-1;\n\n //make the 1st semi-window\n for(int i=1;i<=1+dist - 1;i++){\n insert(nums[i]);\n }\n\n long long min_sum = LLONG_MAX;\n for(int i=1;i<n;i++){\n int j = i+dist;\n if(!(j<n)) break;\n\n insert(nums[j]);//update\n\n balance();\n min_sum = min(min_sum, window_sum);\n\n erase(nums[i]);//update\n }\n return min_sum + nums.front();\n }\n};",
"memory": "166400"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\n\npublic:\n\n // This function calculates the minimum sum of 'k' elements from each consecutive window of 'dist + 1' elements in the 'nums' vector.\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n\n // The small_set keeps the smallest 'k-1' elements, and the large_set keeps the others.\n\n multiset<int> small_set, large_set;\n\n int window_size = dist + 1;\n\n long long window_sum = 0; // Sum of the smallest 'k' elements in the current window.\n\n long long min_sum = 0; // Minimum sum found of 'k' elements across all windows.\n\n\n // Initialize the first window\n\n for (int i = 1; i <= window_size; i++) {\n\n small_set.insert(nums[i]);\n\n window_sum += nums[i];\n\n }\n\n\n // Ensure small_set only has 'k-1' smallest values\n\n while (small_set.size() > k - 1) {\n\n large_set.insert(*small_set.rbegin());\n\n window_sum -= *small_set.rbegin();\n\n small_set.erase(small_set.find(*small_set.rbegin()));\n\n }\n\n min_sum = window_sum;\n\n\n // Slide the window across the array\n\n for (int i = window_size + 1; i < nums.size(); i++) {\n\n window_sum += nums[i];\n\n small_set.insert(nums[i]);\n\n \n\n // Maintain the two multisets by moving the distanced element to the correct set\n\n if (large_set.find(nums[i - window_size]) != large_set.end()) {\n\n large_set.erase(large_set.find(nums[i - window_size]));\n\n } else {\n\n window_sum -= nums[i - window_size];\n\n small_set.erase(small_set.find(nums[i - window_size]));\n\n }\n\n\n // Maintain the size of the small_set ('k-1' elements)\n\n while (small_set.size() > k - 1) {\n\n window_sum -= *small_set.rbegin();\n\n large_set.insert(*small_set.rbegin());\n\n small_set.erase(small_set.find(*small_set.rbegin()));\n\n }\n\n\n // Populate small_set if it has fewer than 'k-1' elements by taking from large_set\n\n while (small_set.size() < k - 1) {\n\n window_sum += *large_set.begin();\n\n small_set.insert(*large_set.begin());\n\n large_set.erase(large_set.begin());\n\n }\n\n\n // Balance the sets, so the elements in small_set are actually smaller than those in large_set\n\n while (!small_set.empty() && !large_set.empty() && *small_set.rbegin() > *large_set.begin()) {\n\n window_sum -= *small_set.rbegin() - *large_set.begin();\n\n small_set.insert(*large_set.begin());\n\n large_set.insert(*small_set.rbegin());\n\n small_set.erase(small_set.find(*small_set.rbegin()));\n\n large_set.erase(large_set.begin());\n\n }\n\n \n\n min_sum = std::min(min_sum, window_sum); // Update the minimum sum found\n\n }\n\n\n // The result is the minimum sum of 'k' elements from each window plus the first element\n\n return nums[0] + min_sum;\n\n }\n\n};",
"memory": "166500"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "//m0\n//more general template with balance().\n\n//dual multiset for top k.\n\n//multiset.count() --> TLE. 'cuz it actually counts.\n\n//O(n log dist)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n multiset<int> chosen, candidate;\n int chosen_k;//k-1\n long long window_sum = 0;\n\n inline void insert(const int& val){\n window_sum += val;\n chosen.insert(val);\n }\n inline void erase_chosen(const int& val){\n window_sum -= val;\n //erase(val) erases all instances\n chosen.erase(chosen.find(val));\n }\n inline void erase(const int& val){\n //multiset.count() --> TLE\n if(chosen.find(val) != chosen.end()){\n erase_chosen(val);\n }else{\n candidate.erase( candidate.find(val) );\n }\n }\n\n inline void balance(){\n while(chosen.size() > chosen_k){\n int val = *chosen.rbegin();\n\n erase_chosen(val);\n\n candidate.insert(val);\n }\n while(chosen.size() < chosen_k && candidate.size()){\n int val = *candidate.begin();\n\n candidate.erase(candidate.begin());\n\n insert(val);\n }\n\n //crucial swapping\n while(chosen.size() && candidate.size() && *chosen.rbegin() > *candidate.begin()){\n int cho = *chosen.rbegin();\n int can = *candidate.begin();\n\n erase_chosen(cho);\n candidate.erase(candidate.begin());\n\n insert(can);\n candidate.insert(cho);\n }\n }\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n chosen_k = k-1;\n\n //make the 1st semi-window\n for(int i=1;i<=1+dist - 1;i++){\n insert(nums[i]);\n }\n\n long long min_sum = LLONG_MAX;\n for(int i=1;i<n;i++){\n int j = i+dist;\n if(!(j<n)) break;\n\n insert(nums[j]);//update\n\n balance();\n min_sum = min(min_sum, window_sum);\n\n erase(nums[i]);//update\n }\n return min_sum + nums.front();\n }\n};",
"memory": "166600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "//m0\n//more general template with balance().\n\n//dual multiset for top k.\n\n//multiset.count() --> TLE. 'cuz it actually counts.\n\n//O(n log dist)\nstatic auto _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n multiset<int> chosen, candidate;\n int chosen_k;//k-1\n long long window_sum = 0;\n\n inline void insert(const int& val){\n window_sum += val;\n chosen.insert(val);\n }\n inline void erase_chosen(const int& val){\n window_sum -= val;\n //erase(val) erases all instances\n chosen.erase(chosen.find(val));\n }\n inline void erase(const int& val){\n //multiset.count() --> TLE\n if(chosen.find(val) != chosen.end()){\n erase_chosen(val);\n }else{\n candidate.erase( candidate.find(val) );\n }\n }\n\n inline void balance(){\n while(chosen.size() > chosen_k){\n int val = *chosen.rbegin();\n\n erase_chosen(val);\n\n candidate.insert(val);\n }\n while(chosen.size() < chosen_k && candidate.size()){\n int val = *candidate.begin();\n\n candidate.erase(candidate.begin());\n\n insert(val);\n }\n\n //crucial swapping\n while(chosen.size() && candidate.size() && *chosen.rbegin() > *candidate.begin()){\n int cho = *chosen.rbegin();\n int can = *candidate.begin();\n\n erase_chosen(cho);\n candidate.erase(candidate.begin());\n\n insert(can);\n candidate.insert(cho);\n }\n }\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n chosen_k = k-1;\n\n //make the 1st semi-window\n for(int i=1;i<=1+dist - 1;i++){\n insert(nums[i]);\n }\n\n long long min_sum = LLONG_MAX;\n for(int i=1;i<n;i++){\n int j = i+dist;\n if(!(j<n)) break;\n\n insert(nums[j]);//update\n\n balance();\n min_sum = min(min_sum, window_sum);\n\n erase(nums[i]);//update\n }\n return min_sum + nums.front();\n }\n};",
"memory": "166600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n ll n=nums.size();\n map<ll,ll> mpmax,mpmin;\n ll res=nums[0],mn=LLONG_MAX;\n // while(i<=n-k+1){\n\n int i=1,j=i;\n ll sum=0;\n for(;j<=k-2+i;j++){\n sum+=nums[j];\n mpmax[nums[j]]++;\n }\n mn=min(mn,sum);\n while(j<=i+dist && j<n){\n sum+=nums[j];\n mpmax[nums[j]]++;\n auto it1=mpmax.end();\n it1--;\n ll num=it1->first;\n sum-=num;\n mpmax[num]--;\n if(mpmax[num]==0){\n mpmax.erase(num);\n }\n mpmin[num]++;\n mn=min(mn,sum);\n j++;\n\n }\n\n for(;i<=n-k+1 && j<n ;i++,j++){\n bool f=false;\n if(mpmax.find(nums[i])!=mpmax.end()){\n mpmax[nums[i]]--;\n if(mpmax[nums[i]]==0){\n mpmax.erase(nums[i]);\n }\n sum-=nums[i];\n f=true;\n }\n else{\n mpmin[nums[i]]--;\n if(mpmin[nums[i]]==0){\n mpmin.erase(nums[i]);\n }\n }\n if(f){\n mpmax[nums[j]]++;\n sum+=nums[j];\n }\n else{\n mpmin[nums[j]]++;\n }\n\n if(!mpmin.empty()){\n auto it=mpmin.begin();\n \n ll num=it->first;\n mpmin[num]--;\n if(mpmin[num]==0){\n mpmin.erase(num);\n }\n mpmax[num]++;\n sum+=num;\n it=mpmax.end();\n it--;\n num=it->first;\n sum-=num;\n mpmax[num]--;\n if(mpmax[num]==0){\n mpmax.erase(num);\n }\n\n mpmin[num]++;\n }\n mn=min(mn,sum);\n\n }\n\n // }\n return mn+res;\n }\n};",
"memory": "167400"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n \n int n=nums.size();\n\n multiset<int>m1;\n multiset<int>m2;\n\n long long ans=nums[0];\n \n k--;\n\n for(int i=1;i<=dist+1;i++){\n m1.insert(nums[i]);\n ans+=nums[i];\n }\n\n\n while(m1.size()>k){\n m2.insert(*m1.rbegin());\n ans-=*m1.rbegin();\n int temp=*m1.rbegin();\n m1.erase(m1.find(temp));\n }\n\n long long maxi=ans;\n\n for(int i=dist+2;i<n;i++){\n\n int pre=nums[i-dist-1];\n if(m1.find(pre)!=m1.end()){\n ans-=pre;\n m1.erase(m1.find(pre));\n }else{\n m2.erase(m2.find(pre));\n }\n\n m1.insert(nums[i]);\n ans+=nums[i];\n while(!m1.empty() && !m2.empty()){\n if(*m2.begin()<*m1.rbegin()){\n int temp1=*m2.begin();\n int temp2=*m1.rbegin();\n m1.erase(m1.find(temp2));\n m1.insert(temp1);\n m2.erase(m2.find(temp1));\n m2.insert(temp2);\n ans-=temp2;\n ans+=temp1;\n }else break;\n }\n\n while(m1.size()>k){\n m2.insert(*m1.rbegin());\n ans-=*m1.rbegin();\n int temp=*m1.rbegin();\n m1.erase(m1.find(temp));\n }\n\n while(m1.size()<k){\n m1.insert(*m2.begin());\n ans+=*m2.begin();\n int temp=*m2.begin();\n m2.erase(m2.find(temp));\n }\n\n maxi=min(maxi,ans);\n\n }\n\n return maxi;\n\n\n }\n};",
"memory": "169900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "#define remove(t, tmp) t.erase(t.find(tmp)) // remove_one_instance_of_tmp_from_t\n#define max_val(t) *t.rbegin()\n#define min_val(r) *r.begin()\n#define exists(t, val) t.find(val) != t.end()\n#define update(t, r, sum) { int tmp = max_val(t); r.insert(tmp), remove(t, tmp), sum -= tmp; }\n\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n long long sum = 0, n = nums.size();\n multiset<int> t, r;\n\n k--; // because nums[0] is already used\n\n // put nums[1.....dist+1] in window\n for(int i = 1; i <= dist+1; i++) sum += nums[i], t.insert(nums[i]);\n\n // remove excess, just keep k in window\n while(t.size() > k) update(t, r, sum)\n \n long long ans = sum;\n for(int i = dist + 2; i < n; i++){\n // removal from the window\n int val = nums[i - dist - 1];\n if(exists(t, val)) remove(t, val), sum -= val;\n else remove(r, val);\n\n // addition in the window\n r.insert(nums[i]);\n if(t.size() < k or min_val(r) < max_val(t)){\n int tmp = min_val(r);\n remove(r, tmp), t.insert(tmp), sum += tmp;\n if(t.size() > k) update(t, r, sum)\n }\n\n // updating the ans\n if(ans > sum) ans = sum;\n }\n return ans + nums[0];\n }\n};",
"memory": "170100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "using LL = long long;\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n /*\n [x x x x x] [i1 x x x x] ... [ik-1 x x x x] \n ik-1 - i1 <=dist\n */\n LL min_cost = LONG_LONG_MAX;\n LL sum = 0;\n int n = nums.size();\n multiset<int> set1;\n multiset<int> set2;\n for(int i=1; i<n; i++){\n if(set1.size()<k-1){\n set1.insert(nums[i]);\n sum += nums[i];\n }\n else{\n if(*set1.rbegin()<=nums[i]) set2.insert(nums[i]);\n else{\n sum -= *set1.rbegin()-nums[i];\n set2.insert(*set1.rbegin());\n set1.erase(prev(set1.end()));\n set1.insert(nums[i]);\n }\n }\n\n if(i>=dist+1){\n min_cost = min(min_cost, sum);\n int prev = nums[i-dist];\n if(set2.find(prev)!=set2.end()) set2.erase(set2.find(prev));\n else{\n sum -= prev;\n set1.erase(set1.find(prev));\n if(!set2.empty()){\n sum += *set2.begin();\n set1.insert(*set2.begin());\n set2.erase(set2.begin());\n }\n }\n }\n }\n return min_cost + nums[0];\n }\n};",
"memory": "170600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "using LL = long long;\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n LL min_cost = LONG_LONG_MAX;\n LL sum = 0;\n int n = nums.size();\n multiset<int> set1;\n multiset<int> set2;\n for(int i=1; i<n; i++){\n if(set1.size()<k-1){\n set1.insert(nums[i]);\n sum += nums[i];\n }\n else{\n if(*set1.rbegin()<=nums[i]) set2.insert(nums[i]);\n else{\n sum -= *set1.rbegin()-nums[i];\n set2.insert(*set1.rbegin());\n set1.erase(prev(set1.end()));\n set1.insert(nums[i]);\n }\n }\n\n if(i>=dist+1){\n min_cost = min(min_cost, sum);\n int prev = nums[i-dist];\n if(set2.find(prev)!=set2.end()) set2.erase(set2.find(prev));\n else{\n sum -= prev;\n set1.erase(set1.find(prev));\n if(!set2.empty()){\n sum += *set2.begin();\n set1.insert(*set2.begin());\n set2.erase(set2.begin());\n }\n }\n }\n }\n return nums[0]+min_cost;\n }\n};",
"memory": "170700"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\n typedef long long int ll;\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n \n ll ans=0;int n =nums.size();\n \n multiset<ll> s;dist += 1;k-=1;\n for(int i=1;i<1+dist;i++){\n s.insert(nums[i]);\n }\n \n // for(auto it:s){\n // cout<<it<<\" \";\n // }cout<<endl;\n \n multiset<int> ans_set;\n while(ans_set.size() < k){\n ans_set.insert(*s.begin());\n ans += *s.begin();\n s.erase(s.begin());\n }\n \n // for(auto it:ans_set){\n // cout<<it<<\" \";\n // }cout<<endl;\n // for(auto it:s){\n // cout<<it<<\" \";\n // }cout<<endl;\n \n int ind = 1;ll curr = ans;\n for(int i=dist+1;i<n;i++){\n if(s.find(nums[ind]) != s.end()){\n s.insert(nums[i]);\n s.erase(s.find(nums[ind]));\n \n if(*s.begin() < *(--ans_set.end())){\n curr = curr-*(--ans_set.end())+*(s.begin());\n ans = min(ans,curr);\n ans_set.insert(*s.begin());\n s.insert(*(--ans_set.end()));\n \n ans_set.erase(ans_set.find(*(--ans_set.end())));\n s.erase(s.find(*s.begin()));\n }\n }\n else if(ans_set.find(nums[ind]) != ans_set.end()){\n \n ans_set.erase(ans_set.find(nums[ind]));\n \n if(!s.empty() && *s.begin()<=nums[i]){\n curr = curr-nums[ind]+*s.begin();\n ans = min(ans,curr);\n ans_set.insert(*s.begin());\n s.erase(s.begin());\n s.insert(nums[i]);\n }\n else{\n ans_set.insert(nums[i]);\n curr = curr-nums[ind]+nums[i];\n ans =min(ans,curr);\n }\n }\n \n ind += 1;\n }\n \n ans += nums[0];\n return ans;\n \n \n }\n};",
"memory": "171600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nclass MedianFinder{\n multiset<int> right;\n multiset<int, greater<int>> left;\n long long sum = 0;\n int k;\npublic:\n MedianFinder(int _k){\n k = _k;\n }\n long long getSum() {\n return sum;\n } \n void balance(){\n if(left.size() > k){\n right.insert(*(left.begin()));\n sum -= *left.begin();\n left.erase(left.begin());\n }\n else if(left.size() < k and right.size()){\n left.insert(*(right.begin()));\n sum += *right.begin();\n right.erase(right.begin());\n }\n }\n void addNum(int num) {\n if(right.size() and num >= *(right.begin())) right.insert(num);\n else{\n left.insert(num);\n sum += num;\n }\n balance();\n }\n void remove(int num){\n if(right.count(num)) right.erase(right.find(num));\n else{\n left.erase(left.find(num));\n sum -= num;\n }\n balance();\n }\n};\n long long minimumCost(vector<int>& nums, int k, int dist) {\n MedianFinder kf(k - 1);\n\n int window = dist + 1;\n int val = nums[0];\n nums.erase(nums.begin());\n\n int n = nums.size();\n for(int i = 0; i < window; i++) kf.addNum(nums[i]);\n long long res = kf.getSum();\n\n for(int i = window; i < n; i++){\n kf.addNum(nums[i]);\n kf.remove(nums[i - window]);\n res = min(res, kf.getSum());\n }\n return res + val;\n }\n};",
"memory": "171700"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n // 1. window of size of dist whose k smallest items have the smallest overall sum\n k = k - 1;\n int n = nums.size();\n set<pair<int, int>> smallest_k, largest;\n long long smallest_sum = std::numeric_limits<long long>::max();\n long long sum = 0;\n for (int i = 1; i < n; ++i) {\n // remove if necessary\n if (i > dist + 1) {\n auto search = make_pair(nums[i - dist - 1], i - dist - 1);\n largest.erase(search);\n if (smallest_k.find(search) != smallest_k.end()) {\n sum -= search.first;\n smallest_k.erase(search);\n }\n\n if (!largest.empty() && smallest_k.size() < k) {\n auto next = largest.begin();\n sum += next->first;\n smallest_k.insert(*next);\n largest.erase(next);\n }\n }\n\n // add if we can\n auto target = make_pair(nums[i], i);\n if (smallest_k.size() < k || nums[i] < smallest_k.rbegin()->first) {\n if (smallest_k.size() == k) {\n auto del = std::prev(smallest_k.end());\n sum -= del->first;\n largest.insert(*del);\n smallest_k.erase(del);\n }\n sum += target.first;\n smallest_k.insert(target);\n }\n else {\n largest.insert(target);\n }\n\n // check if we have new best\n if (i > dist) {\n if (sum < smallest_sum) {\n smallest_sum = sum;\n }\n }\n }\n\n return smallest_sum + nums.front();\n }\n};",
"memory": "172000"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "using LL = long long;\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n // multiset default low to high\n multiset<int> topk, candidates;\n k--;\n LL sum=0, ret=LLONG_MAX;\n int start = 1;\n for(int i=1; i<n; i++){\n if(topk.size() < k){\n // Haven't picked top k smallest\n topk.insert(nums[i]);\n sum += nums[i];\n }\n else if(nums[i] > *topk.rbegin()){\n // New num is greater than than largest num in topk\n // put it into candidates\n candidates.insert(nums[i]);\n }\n else{\n // New num has to push to topk,\n //and move the last num from topk to candidates\n int last = *topk.rbegin();\n // candidates.insert(last);\n sum -= last;\n topk.erase(prev(topk.end()));\n topk.insert(nums[i]);\n candidates.insert(last);\n sum += nums[i];\n }\n //Check if sliding window exceeds dist\n if(i-start >= dist){\n // Update smallest small\n ret = min(ret, sum);\n int first = nums[start];\n start++;\n // remove start element\n \n auto tmp = candidates.find(first);\n //if in candidates\n if(tmp != candidates.end())\n candidates.erase(tmp);\n else{\n auto tmp2 = topk.find(first);\n // if in topk\n if(tmp2 != topk.end()){\n topk.erase(tmp2);\n sum -= first;\n // check if candidates is empty\n if(!candidates.empty()){\n topk.insert(*candidates.begin());\n sum += *candidates.begin();\n candidates.erase(candidates.begin());\n }\n }\n }\n // start++;\n }\n }\n return ret+nums[0];\n }\n};\n\n",
"memory": "172100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n set<pair<int, int>> st1; // {value , index}\n set<pair<int, int>> st2; // {value , index}\n int n = nums.size();\n\n long long ans = 9223372036854775807;\n long long temp = 0;\n int i = 1;\n for(int j = 1; j<n; j++){\n if(st1.empty() || st1.size() < k-1){\n temp += nums[j];\n st1.insert({nums[j], j});\n // cout<<nums[j]<<\"-1-\"<<temp<<endl;\n }\n else if((--st1.end())->first > nums[j]){\n temp -= (--st1.end())->first;\n temp += nums[j];\n int val = (--st1.end())->first;\n int idx = (--st1.end())->second;\n // cout<<val<<\" \"<<idx<<endl;\n st2.insert({val, idx});\n st1.erase({val, idx});\n st1.insert({nums[j], j});\n // cout<<nums[j]<<\"-2-\"<<temp<<endl;\n }\n else{\n st2.insert({nums[j], j});\n // cout<<nums[j]<<\"-3-\"<<temp<<endl;\n }\n\n if(j-i+1 == dist + 1){\n ans = min(ans, temp);\n\n if(st1.find({nums[i], i}) != st1.end()){\n temp -= nums[i];\n st1.erase({nums[i], i});\n if(!st2.empty()){\n int val = st2.begin()->first;\n int idx = st2.begin()->second;\n temp += val;\n st1.insert({val, idx});\n st2.erase({val, idx});\n }\n }\n else if(st2.find({nums[i], i}) != st2.end()) st2.erase({nums[i], i});\n i++;\n }\n // cout<<nums[j]<<\"-\"<<temp<<endl;\n // for(auto x:st1)cout<<x.first<<\" \";\n // cout<<endl;\n // for(auto x:st2)cout<<x.first<<\" \";\n // cout<<endl;\n }\n\n return ans + nums[0];\n }\n};",
"memory": "172300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\nprivate:\n multiset<int> m1, m2;\n ll sum; \n \n void add(int num, int n){\n // cout<<\"add \"<<num<<endl;\n if((int)m1.size() >= n && num >= *(m1.rbegin())){\n m2.insert(num);\n }else{\n m1.insert(num);\n sum += (ll)num;\n \n if((int)m1.size() > n){\n int val = *(m1.rbegin());\n m2.insert(val);\n m1.erase(m1.find(val));\n sum -= (ll)val;\n }\n }\n }\n \n void remove(int num, int n){\n // cout<<\"remove \"<<num<<endl;\n if(num > *(m1.rbegin())){\n m2.erase(m2.find(num));\n }else{\n m1.erase(m1.find(num));\n sum -= (ll)num;\n if(!m2.empty()){\n int val = *(m2.begin());\n m1.insert(val);\n m2.erase(m2.find(val));\n sum += (ll)val;\n }\n }\n }\n \npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n m1.clear();\n m2.clear();\n sum = 0;\n \n ll ans = 1e18;\n \n for(int i=1; i<n; i++){\n add(nums[i], k-1);\n if(i-dist-1>0){\n remove(nums[i-dist-1], k-1);\n }\n \n if(i>=k-1) ans = min(ans, sum);\n // cout<<i<<\" \"<<sum<<\" \"<<ans<<\" \"<<m1.size()<<\" \"<<m2.size()<<endl;\n\n }\n \n ans += (ll)nums[0];\n \n return ans;\n \n }\n};",
"memory": "172500"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "void insrt(long long num, multiset<long long> &sml, multiset<long long> &bg, long long &sum, int k){\n // cout<<num<<endl;\n if(sml.size() < k-1){\n sml.insert(num);\n sum += num;\n }else{\n auto it = sml.end();\n it--;\n if(*it > num){\n sml.insert(num);\n bg.insert(*it);\n sum -= *it;\n sml.erase(it);\n sum += num;\n \n }else{\n bg.insert(num);\n }\n \n }\n // cout<<\"SUM: \"<<sum<<endl;\n}\nvoid erse(long long num, multiset<long long> &sml, multiset<long long> &bg, long long &sum, int k){\n // cout<<num<<endl;\n if(sml.find(num) != sml.end()){\n sml.erase(sml.find(num));\n sum -= num;\n if(!bg.empty()){\n auto it = bg.begin();\n sml.insert(*it);\n sum += *it;\n bg.erase(it);\n }\n }else{\n if(bg.find(num) != bg.end()){\n bg.erase(bg.find(num));\n }\n }\n // cout<<\"SUM: \"<<sum<<endl;\n}\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int l = 1;\n int r = dist+1;\n multiset<long long> sml;\n multiset<long long> bg;\n long long sum = 0;\n for(int i=l; i<r; i++){\n if(sml.size() < k-1){\n sml.insert(nums[i]);\n sum += nums[i];\n }else{\n auto it = sml.end();\n it--;\n if(*it > nums[i]){\n sml.insert(nums[i]);\n bg.insert(*it);\n sum -= *it;\n sml.erase(it);\n sum += nums[i];\n \n }else{\n bg.insert(nums[i]);\n }\n \n }\n }\n long long ans = LLONG_MAX;\n int sz = nums.size();\n // cout<<sz<<endl;\n while(r < sz){\n // cout<<l<<\" \"<<r<<endl;\n insrt(nums[r], sml, bg, sum, k);\n // cout<<sml.size()<<\" \"<<bg.size()<<endl;\n ans = min(ans, sum + nums[0]);\n erse(nums[l], sml, bg, sum, k);\n l++;\n r++;\n }\n return ans;\n \n }\n};",
"memory": "172600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void rearrange(multiset<long long>&m1,multiset<long long>&m2,long long &sum,int k){\n while(m1.size()!=k-1){\n m1.insert(*m2.begin());\n sum+=*m2.begin();\n m2.erase(m2.find(*m2.begin()));\n }\n if(*m1.rbegin()>*m2.begin()){\n m2.insert(*m1.rbegin());\n sum-=*m1.rbegin();\n m1.erase(m1.find(*m1.rbegin()));\n m1.insert(*m2.begin());\n sum+=*m2.begin();\n m2.erase(m2.find(*m2.begin()));\n }\n return ;\n }\n long long minimumCost(vector<int>& nums, int k, int dist) {\n long long ans=1e15,sum=0;\n multiset<long long>m1,m2;\n for(int i=1;i<=1+dist;i++){\n m1.insert(nums[i]);\n sum+=nums[i];\n }\n while(m1.size()>k-1){\n sum-=*m1.rbegin();\n m2.insert(*m1.rbegin());\n m1.erase(m1.find(*m1.rbegin()));\n }\n for(int i=1;i<=nums.size()-(k-1);i++){\n ans=min(ans,sum+nums[0]);\n if(m1.find(nums[i])!=m1.end()){\n sum-=nums[i];\n m1.erase(m1.find(nums[i]));\n if(i+dist+1<nums.size()){\n m1.insert(nums[i+dist+1]);\n sum+=nums[i+dist+1];\n }\n if(i!=nums.size()-(k-1))\n rearrange(m1,m2,sum,k);\n }else{\n if(m2.find(nums[i])!=m2.end())\n m2.erase(m2.find(nums[i]));\n if(i+dist+1<nums.size()){\n m2.insert(nums[i+dist+1]);\n }\n if(i!=nums.size()-(k-1))\n rearrange(m1,m2,sum,k);\n }\n }\n return ans;\n }\n};",
"memory": "174100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minimumCost(std::vector<int>& nums, int k, int dist) {\n // Initializing variables\n long long int initialExpense = nums[0];\n nums.erase(nums.begin());\n int size = nums.size();\n k--;\n dist++;\n std::multiset<long long int> bigSet, smallSet;\n long long int currentSum = 0, minimalSum;\n\n // Adding the first 'dist' elements to the 'smallSet' and calculate their sum\n for (int i = 0; i < dist; i++) {\n smallSet.insert(nums[i]);\n currentSum += nums[i];\n }\n\n // Ensuring that 'smallSet' has 'k' elements by moving the largest elements to 'bigSet'\n int difference = dist - k;\n while (difference--) {\n int maximumElement = *smallSet.rbegin();\n currentSum -= maximumElement;\n bigSet.insert(maximumElement);\n smallSet.erase(smallSet.find(maximumElement));\n }\n\n minimalSum = currentSum;\n std::cout << minimalSum << std::endl; // Output the initial sum\n\n // Iterating through the remaining elements\n for (int i = 1; i <= size - dist; i++) {\n // Checking if the element to be removed is in 'smallSet'\n if (smallSet.find(nums[i - 1]) != smallSet.end()) {\n currentSum -= nums[i - 1];\n smallSet.erase(smallSet.find(nums[i - 1]));\n\n // If 'bigSet' is not empty, move the smallest element from 'bigSet' to 'smallSet'\n if (bigSet.size() != 0) {\n int minimumElement = *bigSet.begin();\n currentSum += minimumElement;\n smallSet.insert(minimumElement);\n bigSet.erase(bigSet.find(minimumElement));\n }\n } else {\n // If the element to be removed is in 'bigSet', simply remove it\n bigSet.erase(bigSet.find(nums[i - 1]));\n }\n\n // Getting the largest element in 'smallSet'\n int lastElement = *smallSet.rbegin();\n\n // Getting the new element to be added\n int newElement = nums[i + dist - 1];\n\n // Checking if 'smallSet' has less than 'k' elements\n if (smallSet.size() < k) {\n currentSum += newElement;\n smallSet.insert(newElement);\n } else {\n // If 'newElement' is smaller than the largest element in 'smallSet'\n if (newElement < lastElement) {\n currentSum -= lastElement;\n currentSum += newElement;\n bigSet.insert(lastElement);\n smallSet.erase(smallSet.find(lastElement));\n smallSet.insert(newElement);\n } else {\n // If 'newElement' is not smaller, adding it to 'bigSet'\n bigSet.insert(newElement);\n }\n }\n\n // Updating the minimal sum\n minimalSum = std::min(minimalSum, currentSum);\n }\n\n // Returning the final result\n return minimalSum + initialExpense;\n }\n};",
"memory": "175100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "struct maxHeap {\n multiset <int> st;\n long long sum;\n maxHeap() {\n sum = 0;\n }\n void erase (int num) {\n assert (st.size());\n sum -= num;\n st.erase (st.find(num));\n }\n int top() {\n assert (st.size());\n return *st.rbegin();\n }\n void pop() {\n assert (st.size());\n erase (top());\n }\n void insert (int num) {\n sum += num;\n st.insert(num);\n }\n int size() {\n return st.size();\n }\n long long tot() {\n return sum;\n }\n};\n\nstruct minHeap {\n multiset <int> st;\n long long sum;\n minHeap() {\n sum = 0;\n }\n void erase (int num) {\n assert (st.size());\n sum -= num;\n st.erase (st.find(num));\n }\n int top() {\n assert (st.size());\n return *st.begin();\n }\n void pop() {\n assert (st.size());\n erase (top());\n }\n void insert (int num) {\n sum += num;\n st.insert(num);\n }\n int size() {\n return st.size();\n }\n long long tot() {\n return sum;\n }\n};\n\n\nstruct minKsum{\n struct maxHeap maxHeap;\n struct minHeap minHeap;\n int k, cnt;\n\n minKsum(int K) {\n k = K;\n cnt = 0;\n }\n\n void balance() {\n while (maxHeap.size() < k && minHeap.size()) {\n int num = minHeap.top();\n minHeap.pop();\n maxHeap.insert(num);\n }\n while (maxHeap.size() > k && maxHeap.size()) {\n int num = maxHeap.top();\n maxHeap.pop();\n minHeap.insert (num);\n }\n }\n\n void insert (int num) {\n if (cnt < k) {\n maxHeap.insert(num);\n } else {\n if (num > maxHeap.top()) {\n minHeap.insert (num);\n } else {\n maxHeap.insert (num);\n }\n }\n cnt += 1;\n balance();\n }\n\n void erase (int num) {\n if (num <= maxHeap.top()) {\n maxHeap.erase(num);\n } else {\n minHeap.erase(num);\n }\n cnt -= 1;\n balance();\n }\n\n long long sum() {\n assert (maxHeap.size() == k);\n return maxHeap.tot();\n }\n};\n\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n /*\n 1st always taken\n kth always taken\n\n (2nd to k-1) \n\n\n */\n int n = nums.size();\n long long ans = 1e18;\n int i = 1;\n struct minKsum q(k-2);\n while (i<k-1) {\n q.insert (nums[i]);\n i += 1;\n }\n\n while (i<n) {\n ans = min(ans, nums[0] + q.sum() + nums[i]);\n q.insert (nums[i]);\n if (i - dist >= 1) {\n q.erase(nums[i-dist]);\n }\n i += 1;\n }\n return ans;\n }\n};",
"memory": "175200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void add(int v, multiset<int>& s, multiset<int>& t, long long& sum, int k) {\n // If the size of set s is less than k - 1, add the element directly to s\n if (s.size() < k - 1) {\n s.insert(v);\n sum += v;\n return;\n }\n\n // If the element is greater than or equal to the maximum element in s, add it to t\n if (v >= *prev(s.end())) {\n t.insert(v);\n return;\n }\n\n // If the element is smaller than the maximum element in s, update the sum and move the maximum element to t\n sum += v - *prev(s.end());\n t.insert(*prev(s.end()));\n s.erase(prev(s.end()));\n s.insert(v);\n }\n\n\n void rem(int v, multiset<int>& s, multiset<int>& t, long long& sum) {\n // If the element is present in t, remove it from t\n if (t.count(v)) {\n t.erase(t.find(v));\n return;\n }\n\n // Subtract the element from the sum and remove it from s\n sum -= v;\n s.erase(s.find(v));\n\n // If t is not empty, add the smallest element from t to s and update the sum\n if (!t.empty()) {\n sum += *t.begin();\n s.insert(*t.begin());\n t.erase(t.begin());\n }\n }\n\n\n\n long long minimumCost(vector<int>& a, int k, int d) {\n int n = a.size();\n long long ans = LLONG_MAX, sum = 0;\n multiset<int> s, t;\n\n // Initialize the multisets and sum for the initial subarray\n for (int i = 1; i <= d + 1; i++) {\n add(a[i], s, t, sum, k);\n }\n\n for (int i = 1; i + k - 2 < n; i++) {\n ans = min(ans, sum + a[0]);\n if (i + d + 1 < n) {\n add(a[i + d + 1], s, t, sum, k);\n }\n rem(a[i], s, t, sum);\n }\n\n return ans;\n }\n};\n",
"memory": "178900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void add(int v, multiset<int>& s, multiset<int>& t, long long& sum, int k) {\n // If the size of set s is less than k - 1, add the element directly to s\n if (s.size() < k - 1) {\n s.insert(v);\n sum += v;\n return;\n }\n\n // If the element is greater than or equal to the maximum element in s, add it to t\n if (v >= *prev(s.end())) {\n t.insert(v);\n return;\n }\n\n // If the element is smaller than the maximum element in s, update the sum and move the maximum element to t\n sum += v - *prev(s.end());\n t.insert(*prev(s.end()));\n s.erase(prev(s.end()));\n s.insert(v);\n }\n\n\n void rem(int v, multiset<int>& s, multiset<int>& t, long long& sum) {\n // If the element is present in t, remove it from t\n if (t.count(v)) {\n t.erase(t.find(v));\n return;\n }\n\n // Subtract the element from the sum and remove it from s\n sum -= v;\n s.erase(s.find(v));\n\n // If t is not empty, add the smallest element from t to s and update the sum\n if (!t.empty()) {\n sum += *t.begin();\n s.insert(*t.begin());\n t.erase(t.begin());\n }\n }\n\n\n\n long long minimumCost(vector<int>& a, int k, int d) {\n int n = a.size();\n long long ans = LLONG_MAX, sum = 0;\n multiset<int> s, t;\n\n // Initialize the multisets and sum for the initial subarray\n for (int i = 1; i <= d + 1; i++) {\n add(a[i], s, t, sum, k);\n }\n\n for (int i = 1; i + k - 2 < n; i++) {\n ans = min(ans, sum + a[0]);\n if (i + d + 1 < n) {\n add(a[i + d + 1], s, t, sum, k);\n }\n rem(a[i], s, t, sum);\n }\n\n return ans;\n }\n};",
"memory": "179200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class MinKSum{\n private:\n multiset<int> a,b;\n long long sum;\n int k;\n public:\n MinKSum(int k){\n this->k=k;\n this->sum=0;\n a.clear();\n b.clear();\n }\n long long query(){\n repair();\n if(a.size()<k)return 1e18;\n return sum;\n }\n void insert(int n){\n b.insert(n);\n }\n void remove(int n){\n if(b.find(n)!=b.end()){\n b.erase(b.find(n));\n return;\n }\n sum-=n;\n a.erase(a.find(n));\n repair();\n }\n void repair(){\n while(a.size()<k && b.size()){\n sum+=*b.begin();\n a.insert(*b.begin());\n b.erase(b.begin());\n }\n while(a.size() && b.size() && (*(a.rbegin()))>*b.begin()){\n sum-=(*(a.rbegin()));\n b.insert((*(a.rbegin())));\n a.erase(a.find(*a.rbegin()));\n sum+=*b.begin();\n a.insert(*b.begin());\n b.erase(b.begin());\n }\n }\n \n \n};\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n MinKSum obj(k-1);\n long long res=1e18,n=nums.size();\n for(int i=0;i<=dist;i++)obj.insert(nums[i]);\n for(int i=dist+1;i<n;i++){\n obj.insert(nums[i]);\n obj.remove(nums[i-dist-1]);\n res=min(res,nums[0]+obj.query());\n }\n \n return res;\n \n }\n};",
"memory": "179900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "#include<iostream>\n#include<vector>\n#include<set>\n\nusing namespace std;\n\n#define ll long long\n\nclass Solution {\npublic:\n void printSet(multiset<ll> values) {\n cout << \"set: \";\n for (auto it = values.begin(); it != values.end(); ++it) {\n cout << *it << \" \";\n }\n\n cout << endl;\n }\n\n void printLine() {\n for (int i = 0; i < 100; ++i)\n cout << \"-\";\n cout << endl;\n }\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n\n int n = nums.size();\n\n k -= 2;\n ll smallestSum = 0;\n multiset<ll> smallest, largest;\n\n for (int i = 2; i <= dist + 1; ++i) {\n smallest.insert(nums[i]);\n smallestSum += nums[i];\n if (smallest.size() > k) {\n auto largestInSmallestPointer = smallest.rbegin();\n ll largestInSmallest = *largestInSmallestPointer;\n\n smallestSum -= largestInSmallest;\n\n smallest.erase(smallest.find(largestInSmallest));\n\n largest.insert(largestInSmallest);\n }\n }\n\n ll minCost = nums[0] + nums[1] + smallestSum;\n\n for (int i = dist + 2; i < n - k + dist; ++i) {\n int j = i - dist;\n\n ll num = nums[j];\n\n auto numInLargest = largest.find(num);\n if (numInLargest != largest.end()) {\n largest.erase(numInLargest);\n } else {\n smallest.erase(smallest.find(num));\n smallestSum -= num;\n }\n\n if (i < n) {\n largest.insert(nums[i]);\n }\n\n if (smallest.size() < k) {\n auto smallestInLargestPointer = largest.begin();\n ll smallestInLargest = *smallestInLargestPointer;\n\n largest.erase(smallestInLargestPointer);\n\n smallest.insert(smallestInLargest);\n smallestSum += smallestInLargest;\n } else if (largest.size() > 0) {\n auto largestInSmallestPointer = smallest.rbegin();\n\n auto smallestInLargestPointer = largest.begin(); \n\n ll largestInSmallest = *largestInSmallestPointer;\n ll smallestInLargest = *smallestInLargestPointer;\n\n if (largestInSmallest > smallestInLargest) {\n smallest.erase(smallest.find(largestInSmallest));\n largest.erase(smallestInLargestPointer);\n\n smallestSum += smallestInLargest - largestInSmallest;\n\n largest.insert(largestInSmallest);\n smallest.insert(smallestInLargest);\n }\n }\n\n // cout << j << \" \";\n // printSet(smallest);\n // printSet(largest);\n // cout << smallestSum << endl;\n // printLine();\n\n ll temp = nums[0] + nums[j] + smallestSum;\n minCost = min(minCost, temp);\n }\n\n return minCost;\n }\n};\n\n",
"memory": "181200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "#define ll long long int \n\nclass fenwick{\n vector<ll>sum;\n public:\n fenwick(ll n){\n sum.resize(n+1,0);\n }\n void add(ll idx,ll e){\n idx++;\n ll n=sum.size()-1;\n for(;idx<=n;idx+=(idx&(-idx))){\n sum[idx]+=e;\n }\n }\n //find sum from 0 upto idx \n ll query(ll idx){\n if(idx<0) return 0;\n ll ans=0;\n idx++;\n for(;idx>0;idx-=(idx&(-idx))) ans+=sum[idx];\n return ans;\n }\n};\n\n\nclass Solution {\n unordered_map<int,int>idx,idxr;\n fenwick *fw,*fwCnt;\n int k;\n ll find_sum(){\n int n=idxr.size();\n if(fwCnt->query(n-1)<k) return LLONG_MAX;\n int l=0,r=n-1;\n int pos;\n while(l<=r){\n int mid=(l+r)>>1;\n if(fwCnt->query(mid)>=k) pos=mid,r=mid-1;\n else l=mid+1;\n }\n ll ans=fw->query(pos-1);\n ll rem=k-fwCnt->query(pos-1);\n return ans+rem*idxr[pos];\n }\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n this->k=k-1;\n int n=nums.size();\n auto nums2=nums;\n sort(nums2.begin(),nums2.end());\n for(int i=0;i<n;i++) idx[nums2[i]]=i,idxr[i]=nums2[i];\n fw=new fenwick(n);\n fwCnt=new fenwick(n);\n ll mn=LLONG_MAX;\n for(int i=n-1;i>0;i--){\n fw->add(idx[nums[i]],nums[i]);\n fwCnt->add(idx[nums[i]],1);\n if(i+dist+1<n){\n fw->add(idx[nums[i+dist+1]],-nums[i+dist+1]);\n fwCnt->add(idx[nums[i+dist+1]],-1);\n }\n mn=min(mn,find_sum());\n }\n return mn+nums[0];\n }\n};",
"memory": "181500"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "#define ll long long int \n\nclass fenwick{\n vector<ll>sum;\n public:\n fenwick(ll n){\n sum.resize(n+1,0);\n }\n void add(ll idx,ll e){\n idx++;\n ll n=sum.size()-1;\n for(;idx<=n;idx+=(idx&(-idx))){\n sum[idx]+=e;\n }\n }\n //find sum from 0 upto idx \n ll query(ll idx){\n if(idx<0) return 0;\n ll ans=0;\n idx++;\n for(;idx>0;idx-=(idx&(-idx))) ans+=sum[idx];\n return ans;\n }\n};\n\n\nclass Solution {\n unordered_map<int,int>idx,idxr;\n fenwick *fw,*fwCnt;\n int k;\n ll find_sum(){\n int n=idxr.size();\n if(fwCnt->query(n-1)<k) return LLONG_MAX;\n int l=0,r=n-1;\n int pos;\n while(l<=r){\n int mid=(l+r)>>1;\n if(fwCnt->query(mid)>=k) pos=mid,r=mid-1;\n else l=mid+1;\n }\n ll ans=fw->query(pos-1);\n ll rem=k-fwCnt->query(pos-1);\n return ans+rem*idxr[pos];\n }\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n this->k=k-1;\n int n=nums.size();\n auto nums2=nums;\n sort(nums2.begin(),nums2.end());\n for(int i=0;i<n;i++) idx[nums2[i]]=i,idxr[i]=nums2[i];\n fw=new fenwick(n);\n fwCnt=new fenwick(n);\n ll mn=LLONG_MAX;\n for(int i=n-1;i>0;i--){\n fw->add(idx[nums[i]],nums[i]);\n fwCnt->add(idx[nums[i]],1);\n if(i+dist+1<n){\n fw->add(idx[nums[i+dist+1]],-nums[i+dist+1]);\n fwCnt->add(idx[nums[i+dist+1]],-1);\n }\n mn=min(mn,find_sum());\n }\n return mn+nums[0];\n }\n};",
"memory": "181600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n long long res = 0;\n\n multiset<int> kSmall, rem(nums.begin() + 1, nums.begin() + dist + 2);\n // for(int i = 1; i <= dist + 1; i++)\n // rem.insert(nums[i]);\n\n for(int i = 1; i < k; i++)\n {\n int t = *(rem.begin());\n rem.erase(rem.begin());\n kSmall.insert(t);\n res += t;\n }\n\n long long sum = res;\n for(int i = dist + 2; i < n; i++)\n {\n // add element to the window\n if(nums[i] >= *(kSmall.rbegin()))\n rem.insert(nums[i]);\n else\n {\n int t = *(kSmall.rbegin());\n kSmall.erase(kSmall.find(t));\n rem.insert(t);\n kSmall.insert(nums[i]);\n\n sum += nums[i] - t;\n }\n\n // remove element from window\n int remove = nums[i - (dist + 1)];\n\n if(rem.find(remove) != rem.end())\n rem.erase(rem.find(remove));\n else\n {\n int t = *(rem.begin());\n rem.erase(rem.begin());\n kSmall.erase(kSmall.find(remove));\n kSmall.insert(t);\n\n sum += t - remove;\n }\n\n res = min(res, sum);\n }\n\n return nums[0] + res;\n }\n};",
"memory": "181800"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n=nums.size();\n multiset<int> s0(nums.end()-(dist+1),nums.end()),s1;\n long long sum0=accumulate(nums.end()-(dist+1),nums.end(),0LL);\n while(s0.size()>=k)\n {\n auto it=prev(s0.end());\n sum0-=*it;\n s1.insert(*it);\n s0.erase(it);\n }\n long long ans=sum0;\n for(int i=n-(dist+1)-1,j=n-1;i>0;--i,--j)\n {\n if(auto it=s1.find(nums[j]);it!=s1.end())\n {\n s1.erase(it);\n }\n else\n {\n s0.erase(s0.find(nums[j]));\n sum0-=nums[j];\n if(!s1.empty())\n {\n sum0+=*s1.begin();\n s0.insert(*s1.begin());\n s1.erase(s1.begin());\n }\n }\n sum0+=nums[i];\n s0.insert(nums[i]);\n while(s0.size()>=k)\n {\n auto it=prev(s0.end());\n sum0-=*it;\n s1.insert(*it);\n s0.erase(it);\n }\n ans=min(ans,sum0);\n }\n ans+=nums[0];\n return ans;\n }\n};",
"memory": "181900"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<int> N;\n int n;\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n N = nums;\n // k++;\n n = N.size();\n #define int long long\n multiset<int> S;\n multiset<int> A;\n int sum = 0;\n\n int ans = 1e18;\n \n\n int lim = k-2;\n // k++;\n\n int prev = 1+dist;\n // cout<<prev<<\" \"<<N[prev]<<\" JJ\\n\";\n \n for(int j = 2; j <= min(1+dist, n); j++){\n \n if(S.size() < lim){\n S.insert(N[j]);\n sum = sum + N[j];\n }\n else{\n auto it = S.end();\n it--;\n int num = (*it);\n if(num > N[j]){\n S.erase(it);\n sum = sum - num;\n S.insert(N[j]);\n sum = sum + N[j];\n A.insert(num);\n }\n else A.insert(N[j]);\n }\n // cout<<\"BBBBBBBBBBBBBBBBBB\\n\";\n // for(auto u: S) cout<<u<<\" \";\n // cout<<endl;\n\n }\n\n \n // for(auto u: A) cout<<u<<\" \";\n // cout<<endl;\n\n // cout<<sum<<\" AA\\n\";\n\n for(int i = 1; i < n; i++){\n // int now = N[i];\n // cout<<S.size()<<\" MMMMMMMMMMMMMMMMM\\n\";\n if(S.size() < lim) break;\n // cout<<i<<\" CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\\n\";\n // // cout<<sum+N[i]+N[0]<<\" AA\\n\";\n // cout<<\"BBBBBBBBBBBBBBBBBB\\n\";\n // for(auto u: S) cout<<u<<\" \";\n // cout<<endl;\n // cout<<\" CCCCCCCCCCCCCCCCCCC\\n\";\n // for(auto u: A) cout<<u<<\" \";\n // cout<<endl;\n\n ans = min(ans, N[i] + sum + N[0]);\n prev++;\n\n if(prev < n){\n // cout<<\"INSERTING \"<<N[prev]<<endl; \n if(S.size() < lim){\n S.insert(N[prev]);\n sum = sum + N[prev];\n }\n else{\n auto it = S.end();\n it--;\n int num = (*it);\n if(num > N[prev]){\n S.erase(it);\n sum = sum - num;\n S.insert(N[prev]);\n sum = sum + N[prev];\n A.insert(num);\n }\n else A.insert(N[prev]);\n }\n }\n\n // cout<<\"ZZZZZZZZZZZZZZZZZZZZZ\\n\";\n // for(auto u: S) cout<<u<<\" \";\n // cout<<endl;\n // cout<<\" YYYYYYYYYYYYYYYYYYYYYYYYYYYY\\n\";\n // for(auto u: A) cout<<u<<\" \";\n // cout<<endl;\n\n\n if(i+1 >= n) break;\n\n // cout<<\"REMOVING \"<<N[i+1]<<endl;\n \n if(A.find(N[i+1]) != A.end()){\n A.erase(A.find(N[i+1]));\n }\n else {\n S.erase(S.find(N[i+1]));\n sum = sum - N[i+1];\n auto it = A.begin();\n if(it == A.end()) break;\n int num = *it;\n S.insert(num);\n A.erase(it);\n sum = sum + num;\n }\n\n \n }\n #undef int\n\n return ans;\n }\n};",
"memory": "182300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n multiset<long long> minVals;\n multiset<long long> otherVals;\n long long minValSum = 0;\n vector<long long> ans(n, 1e18);\n // long long returnVal = LLONG_MAX;\n long long returnVal = 1e15;\n for(int i = n-1; i >= 2; i--){\n // cout << \"MinVals: \" << endl;\n // for(auto &ele: minVals) cout << ele << \" \";\n // cout << endl;\n if(minVals.size() < k-2){\n minVals.insert(nums[i]);\n minValSum += nums[i];\n if(minVals.size() == k-2){\n ans[i-1] = nums[0] + nums[i-1] + minValSum;\n returnVal = min(returnVal, ans[i-1]);\n // cout << i << \" \" << returnVal << \" \" << nums[i-1] << \" \" << minValSum << \" \" << nums[0] << endl;\n }\n }\n else{\n if(i+dist < n){\n int delVal = nums[i+dist];\n if(minVals.find(delVal) != minVals.end()) {\n minVals.erase(minVals.find(delVal));\n minValSum -= delVal;\n }\n if(otherVals.find(delVal) != otherVals.end()) {\n otherVals.erase(otherVals.find(delVal));\n }\n }\n int maxVal = 1e9;\n if(minVals.size() > 0) maxVal = *minVals.rbegin();\n otherVals.insert(nums[i]);\n int addVal = *otherVals.begin();\n if(nums[i] < maxVal && minVals.size() == k-2){\n minValSum -= maxVal;\n minVals.erase(minVals.find(maxVal));\n if(maxVal != 1e9) otherVals.insert(maxVal);\n minValSum += addVal;\n minVals.insert(addVal);\n otherVals.erase(otherVals.find(addVal));\n }\n else if (minVals.size() < k-2){\n minValSum += addVal;\n minVals.insert(addVal);\n otherVals.erase(otherVals.find(addVal));\n }\n ans[i-1] = nums[0] + nums[i-1] + minValSum;\n returnVal = min(returnVal, ans[i-1]);\n // cout << i << \" \" << returnVal << \" \" << nums[i-1] << \" \" << minValSum << \" \" << nums[0] << endl;\n }\n }\n return returnVal;\n }\n};",
"memory": "182600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n long long minimumCost(vector<int>& nums, int k, int d) {\n \n long long res = INT_MAX;\n int n = nums.size();\n \n \n multiset<int> s1,s2;\n \n long long sum = nums[0];\n \n for(int i = 1 ; i <= d+1 ; i++){\n \n s1.insert(nums[i]);\n sum += nums[i];\n \n if(s1.size() >= k)\n {\n auto it = --s1.end();\n sum -= *it;\n \n s2.insert(*it);\n s1.erase(it);\n }\n \n\n }\n \n res = sum;\n \n for(int i = 2; i < n-d ; i++){\n \n int a = nums[d+i];\n int r = nums[i-1];\n \n \n if(s1.find(r) != s1.end()){\n \n sum -= r;\n s1.erase(s1.find(r));\n \n \n if(!s2.empty()){\n auto it = s2.begin();\n \n s1.insert(*it);\n \n sum += *it;\n s2.erase(it);\n }\n \n }else{\n s2.erase(s2.find(r));\n }\n\n\n s1.insert(a);\n sum += a;\n \n if(s1.size() >= k)\n {\n auto it = --s1.end();\n sum -= *it;\n \n s2.insert(*it);\n s1.erase(it);\n }\n \n res = min(res, sum);\n }\n \n \n \n return res;\n \n }\n};",
"memory": "183100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n typedef long long LL;\n int n = nums.size();\n LL ans = LLONG_MAX, sum = 0;\n set<pair<int, int>> lft, rht;\n for (int i = 1; i < n; ++i) {\n lft.insert({nums[i], i});\n sum += nums[i];\n if (lft.size() >= k) {\n auto it = *lft.rbegin();\n sum -= it.first;\n rht.insert(it);\n lft.erase(it);\n }\n if (i - dist > 0) {\n ans = min(ans, sum);\n pair<int, int> it = {nums[i - dist], i - dist};\n if (lft.count(it)) {\n sum -= it.first;\n lft.erase(it);\n if (!rht.empty()) {\n it = *rht.begin();\n sum += it.first;\n lft.insert(it);\n rht.erase(it);\n }\n } else\n rht.erase(it);\n }\n }\n ans += nums[0];\n return ans;\n }\n};",
"memory": "183200"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "typedef long long int ll;\n\n#define pii pair<ll, ll>\n#define F first\n#define S second\n\nclass Solution {\n \npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n ll ans = 1e18;\n \n set<pii> minK, rest;\n ll sum = 0;\n \n for (int i = 1; i < n; i ++) {\n minK.insert({nums[i], i});\n sum += nums[i];\n \n if (minK.size() >= k) {\n auto it = *minK.rbegin();\n sum -= it.F;\n rest.insert(it);\n minK.erase(it);\n }\n \n if (i - dist > 0) {\n ans = min (ans, sum);\n \n pii to_remove = {nums[i-dist], i-dist};\n if (minK.count(to_remove)) {\n minK.erase(to_remove);\n sum -= to_remove.F;\n \n if (!rest.empty()) {\n auto it = *rest.begin();\n minK.insert(it);\n sum += it.F;\n rest.erase(it);\n }\n }\n else {\n rest.erase(to_remove);\n }\n }\n }\n return ans+nums[0];\n }\n};",
"memory": "183300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct bag{\n long long sum ;\n int k ;\n multiset<long long >mt1;\n multiset<long long >mt2;\n bag(int _k){\n sum = 0 ;\n k = _k ;\n }\n\n void insert(int x){\n sum += x;\n mt1.insert(x);\n if(mt1.size() > k){\n // cout<<\"hi \"<<k<<\" \"<<x<<endl;\n auto it = mt1.rbegin();\n // cout<<*it<<endl;\n sum -= *it ;\n // cout<< sum<<endl;\n mt2.insert(*it);\n mt1.erase(mt1.find(*it));\n }\n // for(auto x : mt1)cout<<x<<\" \";\n // cout<<endl;\n // for(auto x : mt2)cout<<x<<\" \";\n // cout<<endl;\n }\n\n void delte(int y){\n if(mt1.find(y) != mt1.end()){\n // cout<<sum<<endl;\n mt1.erase(mt1.find(y));\n // cout<<y<<\" vyop\"<<endl;\n sum -= y;\n // cout<<sum<<endl;\n // mt2.insert(y);\n \n if(mt1.size() < k && !mt2.empty()){\n // cout<<\"hi \"<<endl; \n auto it = mt2.begin();\n sum += *it ;\n // cout<<*it<<endl;\n mt1.insert(*it);\n mt2.erase(mt2.find(*it));\n }\n\n // mt2.insert(y);\n }\n else{\n if(mt2.find(y) != mt2.end())mt2.erase(mt2.find(y));\n }\n }\n\n long long getsum(){\n return sum ;\n }\n\n\n \n };\n\n long long minimumCost(vector<int>& nums, int k, int dist) {\n priority_queue<pair<int,int>>pq;\n int n = nums.size();\n long max_size = k-2;\n long long sum =0 ;\n long long ans = nums[0];\n long long res = 1e18;\n bag b(max_size) ;\n for(int i =n-1 ; i>=1 ; i--){\n\n if( (i+dist+1) < n ){\n // cout<<i<<\" emter \"<<endl;\n // cout<<b.getsum()<<endl;\n if((i+dist+1)<n)b.delte(nums[i+dist+1]);\n // cout<<b.getsum()<<endl;\n }\n\n if(i <= (n-k+1))res = min(res,ans+nums[i]+b.getsum());\n // cout<<i<<\" \"<<b.getsum()<<endl;\n // cout<<i<<\" emter \"<<endl; \n b.insert(nums[i]);\n\n }\n\n return res;\n }\n};",
"memory": "183700"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\n typedef long long ll;\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n=nums.size();\n multiset<ll>set1,set2;\n ll sum=0;\n for(int i=1;i<=dist+1;i++){\n set1.insert(nums[i]);\n sum+=nums[i];\n if(set1.size()>k-1){\n auto it=(set1.rbegin());\n sum-=*it;\n set2.insert(*it);\n set1.erase(set1.find(*it));\n }\n }\n ll ans=sum;\n for(int i2=2;i2+dist<n;i2++){\n int s=i2,e=min(n-1,i2+dist);\n set1.insert(nums[e]);\n sum+=nums[e];\n if(set1.size()>k-1){\n auto it=(set1.rbegin());\n sum-=*it;\n set2.insert(*it);\n set1.erase(set1.find(*it));\n }\n if(set1.find(nums[s-1])!=set1.end()){\n sum-=nums[s-1];\n set1.erase(set1.find(nums[s-1]));\n if(!set2.empty()){\n int val=*(set2.begin());\n set2.erase(set2.find(val));\n set1.insert(val);\n sum+=val;\n }\n }\n else{\n set2.erase(set2.find(nums[s-1]));\n }\n ans=min(ans,(ll)(sum));\n }\n return ans+nums[0];\n }\n};",
"memory": "183800"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution\n{\npublic:\n\tlong long minimumCost(vector<int> &nums, int k, int dist)\n\t{\n\t\tmultiset<int> s1, s2;\n\n\t\tlong long ans = nums[0];\n\t\tint t = k - 1;\n\n\t\t// Initialize the sets and answer.\n\t\tfor (int i = 1; i <= min((int)nums.size() - 1, 1 + dist); i++)\n\t\t{\n\t\t\tif (s1.size() < t)\n\t\t\t{\n\t\t\t\tans += nums[i];\n\t\t\t\ts1.insert(nums[i]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint lastel = *s1.rbegin(); // Largest element in s1\n\t\t\t\tif (nums[i] < lastel)\n\t\t\t\t{\n\t\t\t\t\tans = ans + nums[i] - lastel;\n\t\t\t\t\ts2.insert(lastel);\n\t\t\t\t\ts1.erase(s1.find(lastel));\n\t\t\t\t\ts1.insert(nums[i]);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ts2.insert(nums[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tlong long minans = ans;\n\n\t\t// Sliding window starts here.\n\t\tfor (int i = 2 + dist; i < nums.size(); i++)\n\t\t{\n\t\t\tint delel = nums[i - dist - 1];\n\n\t\t\t// Handle deletion from s1 or s2\n\t\t\tauto it = s1.find(delel);\n\t\t\tif (it != s1.end())\n\t\t\t{\n\t\t\t\tans -= delel;\n\t\t\t\ts1.erase(it);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ts2.erase(s2.find(delel));\n\t\t\t}\n\n\t\t\t// Insert new element in s2\n\t\t\ts2.insert(nums[i]);\n\n\t\t\t// Balance sets\n\t\t\twhile (!s1.empty() && !s2.empty() && *s1.rbegin() > *s2.begin())\n\t\t\t{\n\t\t\t\tint a = *s1.rbegin();\n\t\t\t\tint b = *s2.begin();\n\t\t\t\tans = ans - a + b;\n\t\t\t\ts1.erase(prev(s1.end())); // Erase largest from s1\n\t\t\t\ts2.erase(s2.begin()); // Erase smallest from s2\n\t\t\t\ts1.insert(b);\n\t\t\t\ts2.insert(a);\n\t\t\t}\n\n\t\t\t// Ensure size of s1 is maintained at 't'\n\t\t\twhile (s1.size() < t && !s2.empty())\n\t\t\t{\n\t\t\t\tint el = *s2.begin();\n\t\t\t\ts1.insert(el);\n\t\t\t\ts2.erase(s2.begin());\n\t\t\t\tans += el;\n\t\t\t}\n\n\t\t\t// Update the minimum answer\n\t\t\tminans = min(ans, minans);\n\t\t}\n\n\t\treturn minans;\n\t}\n};\n",
"memory": "185000"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "\nclass Solution\n{\npublic:\n\tlong long minimumCost(vector<int> &nums, int k, int dist)\n\t{\n\t\tmultiset<int> s1, s2;\n\t\tlong long ans = nums[0];\n\t\tint t = k - 1;\n\n\n\t\tfor (int i = 1; i <= min((int)nums.size() - 1, 1 + dist); i++)\n\t\t{\n\t\t\tif (s1.size() < t)\n\t\t\t{\n\t\t\t\tans += nums[i];\n\t\t\t\ts1.insert(nums[i]);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tint lastel = *s1.rbegin();\n\t\t\t\tif (nums[i] < lastel)\n\t\t\t\t{\n\t\t\t\t\tans = ans + nums[i] - lastel;\n\t\t\t\t\ts2.insert(lastel);\n\t\t\t\t\ts1.insert(nums[i]);\n\t\t\t\t\ts1.erase(s1.find(lastel));\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\ts2.insert(nums[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tlong long minans = ans;\n\n\t\tfor (int i = 2 + dist; i < nums.size(); i++)\n\t\t{\n\t\t\tint delel = nums[i - dist - 1];\n\n\t\t\tauto it = s1.find(delel);\n\t\t\tif (it != s1.end())\n\t\t\t{\n\t\t\t\tans -= delel;\n\t\t\t\ts1.erase(it);\n\t\t\t}else{\n s2.erase(s2.find(delel));\n }\n\n\t\t\ts2.insert(nums[i]);\n\t\t\twhile (s1.size() and s2.size() and *s1.rbegin() > *s2.begin())\n\t\t\t{\n\t\t\t\tint a = *s1.rbegin();\n\t\t\t\tint b = *s2.begin();\n\t\t\t\tans = ans - a + b;\n\t\t\t\ts1.erase(prev(s1.end()));\n\t\t\t\ts2.erase(s2.begin());\n\t\t\t\ts1.insert(b);\n\t\t\t\ts2.insert(a);\n\t\t\t}\n\t\t\twhile (s1.size() < t and s2.size())\n\t\t\t{\n\t\t\t\tint el = *s2.begin();\n\t\t\t\ts1.insert(el);\n\t\t\t\ts2.erase(s2.begin());\n\t\t\t\tans += el;\n\t\t\t}\n\n\t\t\tminans = min(ans, minans);\n\t\t}\n\n\t\treturn minans;\n\t}\n};\n",
"memory": "185100"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "// class Solution {\n// public:\n// long long minimumCost(vector<int>& nums, int k, int dist) {\n// int n=nums.size();\n// multiset<long long>st1,st2;\n// for(int i=1;i<=dist+1;i++){\n// st1.insert(nums[i]);\n// if(st1.size()>k-1){\n// auto it=st1.end();\n// it--;\n// st2.insert(*it);\n// st1.erase(it);\n// }\n// }\n// long long curr_sum=0;\n// for(auto it:st1) curr_sum+=(long long)it;\n// curr_sum+=nums[0];\n// long long ans=(long long)curr_sum;\n// for(int i=dist+2;i<n;i++){\n// auto it1=st1.find(nums[i-dist-1]);\n// if(it1!=st1.end()){\n// curr_sum-=(long long)*it1;\n// st1.erase(it1);\n// curr_sum+=(long long)nums[i];\n// st1.insert(nums[i]);\n// auto it2=st2.begin();\n// if(it2!=st2.end()){\n// st1.insert(*it2);\n// curr_sum+=(long long)*it2;\n// auto it3=st1.end();\n// it3--;\n// curr_sum-=(long long)*it3;\n// st2.insert(*it3);\n// st1.erase(it3);\n// }\n// ans=min(ans,curr_sum);\n// }else{\n// auto it4=st2.find(nums[i-dist-1]);\n// st2.erase(it4);\n// st1.insert(nums[i]);\n// curr_sum+=(long long)nums[i];\n// auto it3=st1.end();\n// it3--;\n// curr_sum-=(long long)*it3;\n// st2.insert(*it3);\n// st1.erase(it3);\n// ans=min(ans,curr_sum);\n// }\n// cout<<i<<' '<<ans<<\"\\n\";\n// }\n// return ans;\n// }\n// };\n\n\n\nclass Solution {\npublic:\n #define ll long long\n long long minimumCost(vector<int>& nums, int k, int dist) {\n multiset<ll> cur,extra;\n \n ll ans = 0;\n \n \n for(int i=1;i<=dist+1;i++){\n extra.insert(nums[i]);\n }\n ll sum = 0;\n \n auto it = extra.begin();\n for(int i=0;i<k-1;i++){\n it = extra.begin();\n sum+= *it;\n cur.insert(*it);\n extra.erase(it);\n }\n \n ans = sum;\n \n for(int i=dist+2;i<nums.size();i++){\n // cout<<i<<\" -- \";\n ll x = nums[i-dist-1];\n \n it = extra.find(x);\n \n if(it != extra.end()){\n extra.erase(it);\n }\n else{\n // for(auto l:cur)\n // cout<<l<<\" \";\n // cout<<\"\\n\";\n it = cur.find(x);\n cur.erase(it);\n sum-=x;\n }\n \n extra.insert(nums[i]);\n \n it = extra.begin();\n \n sum+= (*it);\n \n cur.insert(*it);\n \n extra.erase(it);\n \n if(cur.size() > k-1){\n x = *cur.rbegin();\n sum-=x;\n cur.erase(cur.find(x));\n \n extra.insert(x);\n }\n \n ans = min(ans, sum);\n }\n return ans + nums[0];\n \n }\n};",
"memory": "186300"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n #define ll long long\n long long minimumCost(vector<int>& nums, int k, int dist) {\n ll ans = 0;\n k--;\n ll maxx = 1e18;\n multiset<ll>st;\n multiset<ll>rem;\n for(int i = 1; i<=dist+1; i++){\n st.insert(nums[i]);\n ans += nums[i];\n }\n while(st.size()>k){\n ans -= *st.rbegin();\n rem.insert(*st.rbegin());\n st.erase(st.find(*st.rbegin()));\n }\n\n int j = 1;\n maxx = ans;\n for(int i = dist+2; i<nums.size(); i++){\n if(rem.find(nums[j]) != rem.end()){\n rem.erase(rem.find(nums[j]));\n }else{\n ans -= nums[j];\n st.erase(st.find(nums[j]));\n }\n j++;\n\n\n rem.insert(nums[i]);\n\n while(st.size() && rem.size() && *rem.begin()<*st.rbegin()){\n ans -= *st.rbegin();\n ans += *rem.begin();\n st.insert(*rem.begin());\n rem.erase(rem.find(*rem.begin()));\n rem.insert(*st.rbegin());\n st.erase(st.find(*st.rbegin()));\n }\n\n while(st.size()<k){\n ans += *rem.begin();\n st.insert(*rem.begin());\n rem.erase(rem.find(*rem.begin())); \n }\n\n maxx = min(maxx,ans);\n }\n \n \n return maxx+nums[0];\n }\n};",
"memory": "186400"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "\nclass MyStructure {\nprivate:\n\tmultiset<int> M1, M2;\n\tlong long sum;\n\tint K;\n\npublic:\n\tMyStructure(int K) : K(K), sum(0LL) {};\n\n\tvoid add(int x) {\n\t\tM1.insert(x);\n\t\tsum += x;\n\t\tif((int)M1.size() > K) {\n\t\t\tint y = *(M1.begin());\n\t\t\tM1.erase(M1.find(y));\n\t\t\tsum -= y;\n\t\t\tM2.insert(y);\n\t\t}\n\t\treturn;\n\t}\n\n\tvoid remove(int x) {\n\t\tif(M2.find(x) != M2.end()) {\n\t\t\tM2.erase(M2.find(x));\n\t\t}\n\t\telse if(M1.find(x) != M1.end()) {\n\t\t\tsum -= x;\n\t\t\tM1.erase(M1.find(x));\n\t\t\tif(M2.empty()) return;\n\t\t\tint y = *(M2.rbegin());\n\t\t\tsum += y;\n\t\t\tM1.insert(y);\n\t\t\tM2.erase(M2.find(y));\n\t\t}\n\t\treturn;\n\t}\n\n\tlong long getSum() {\n\t\treturn sum;\n\t}\n};\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n long long ans = 1e18;\n\n MyStructure pq(k - 2);\n\n for(int i = 2; i < min(n, dist + 2); i++) {\n pq.add(-nums[i]);\n }\n\n ans = min(ans, nums[0] + nums[1] - pq.getSum());\n\n //cout << ans << endl;\n\n for(int i = 2; i < n; i++) {\n if (n - 1 - i + 1 < k - 1) continue;\n pq.remove(-nums[i]);\n if (i + dist < n) pq.add(-nums[i + dist]);\n ans = min(ans, nums[0] + nums[i] - pq.getSum());\n }\n\n return ans;\n\n\n \n }\n};",
"memory": "189500"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "\nclass MyStructure {\nprivate:\n\tmultiset<int> M1, M2;\n\tlong long sum;\n\tint K;\n\npublic:\n\tMyStructure(int K) : K(K), sum(0LL) {};\n\n\tvoid add(int x) {\n\t\tM1.insert(x);\n\t\tsum += x;\n\t\tif((int)M1.size() > K) {\n\t\t\tint y = *(M1.begin());\n\t\t\tM1.erase(M1.find(y));\n\t\t\tsum -= y;\n\t\t\tM2.insert(y);\n\t\t}\n\t\treturn;\n\t}\n\n\tvoid remove(int x) {\n\t\tif(M2.find(x) != M2.end()) {\n\t\t\tM2.erase(M2.find(x));\n\t\t}\n\t\telse if(M1.find(x) != M1.end()) {\n\t\t\tsum -= x;\n\t\t\tM1.erase(M1.find(x));\n\t\t\tif(M2.empty()) return;\n\t\t\tint y = *(M2.rbegin());\n\t\t\tsum += y;\n\t\t\tM1.insert(y);\n\t\t\tM2.erase(M2.find(y));\n\t\t}\n\t\treturn;\n\t}\n\n\tlong long getSum() {\n\t\treturn sum;\n\t}\n};\nclass Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n long long ans = 1e18;\n\n MyStructure pq(k - 2);\n\n for(int i = 2; i < min(n, dist + 2); i++) {\n pq.add(-nums[i]);\n }\n\n ans = min(ans, nums[0] + nums[1] - pq.getSum());\n\n cout << ans << endl;\n\n for(int i = 2; i < n; i++) {\n if (n - 1 - i + 1 < k - 1) continue;\n pq.remove(-nums[i]);\n if (i + dist < n) pq.add(-nums[i + dist]);\n ans = min(ans, nums[0] + nums[i] - pq.getSum());\n }\n\n return ans;\n\n\n \n }\n};",
"memory": "189600"
} |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>k</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>, such that the difference between the starting index of the <strong>second</strong> subarray and the starting index of the <code>kth</code> subarray should be <strong>less than or equal to</strong> <code>dist</code>. In other words, if you divide <code>nums</code> into the subarrays <code>nums[0..(i<sub>1</sub> - 1)], nums[i<sub>1</sub>..(i<sub>2</sub> - 1)], ..., nums[i<sub>k-1</sub>..(n - 1)]</code>, then <code>i<sub>k-1</sub> - i<sub>1</sub> <= dist</code>.</p>
<p>Return <em>the <strong>minimum</strong> possible sum of the cost of these</em> <em>subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,2,6,4,2], k = 3, dist = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,1,2,2,2,1], k = 4, dist = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,8,18,9], k = 3, dist = 1
<strong>Output:</strong> 36
<strong>Explanation:</strong> The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because i<sub>k-1</sub> - i<sub>1</sub> is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between i<sub>k-1</sub> and i<sub>1</sub> is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>3 <= k <= n</code></li>
<li><code>k - 2 <= dist <= n - 2</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n long long ans = LLONG_MAX ; \n int n = nums.size() ; \n // sum of k least element of dist length subarrays\n multiset<int>least_k ,other ; \n int i = 1, j = 0 ; \n long long leastsum = 0 ; \n while(i<=(n-k+1)){\n while(j+1<n && j+1-i<=dist){\n j++ ; \n least_k.insert(nums[j]) ; \n leastsum += nums[j] ; \n if(least_k.size() > k-1){\n other.insert(*least_k.rbegin()) ; \n leastsum -=*least_k.rbegin() ; \n least_k.erase(least_k.find(*least_k.rbegin())) ; \n }\n // cout<<j<<\"?\"<<endl; \n\n }\n // cout<<i<<\"--\"<<endl; \n if(j-i+1>=k-1){\n ans = min(ans,leastsum) ; \n }\n if(j>=i){\n if(other.find(nums[i])!=other.end()){\n other.erase(other.find(nums[i])) ; \n i++ ;\n }\n else {\n least_k.erase(least_k.find(nums[i])) ;\n leastsum-= nums[i] ; \n if(other.size()){\n least_k.insert(*other.begin()) ; \n leastsum+= *other.begin() ; \n other.erase(other.find(*other.begin())) ; \n }\n i++ ; \n \n }\n \n }\n else{\n i+=1; \n j = i-1 ; \n }\n }\n return ans+ nums[0] ; \n }\n};",
"memory": "189800"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int ans = 0;\n int push = 1;\n for(int i = 0;i<word.size();i++){\n if(i==8|| i==16||i==24) push++;\n ans = ans + push;\n }\n return ans;\n \n }\n};",
"memory": "7700"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n switch (word.size())\n {\n case 0:\n case 1:\n case 2:\n case 3:\n case 4:\n case 5:\n case 6:\n case 7:\n case 8: return word.size();\n case 9: \n case 10: \n case 11:\n case 12:\n case 13:\n case 14:\n case 15:\n case 16: return 8+2*(word.size()-8);\n case 17:\n case 18: \n case 19:\n case 20:\n case 21:\n case 22:\n case 23:\n case 24: return 8+16+3*(word.size()-16);\n default: return 8+16+24+4*(word.size()-24);\n\n }\n }\n};",
"memory": "7800"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int n = word.size();\n int presses = 0;\n int order = 1;\n\n while(n >0){\n if(n>=8){\n presses += 8*order;\n order++;\n n -= 8;\n }\n else{\n presses += n*order;\n n = 0;\n }\n }\n\n return presses;\n }\n};",
"memory": "7900"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int n = word.size();\n int press = 1;\n int completePass = n/8;\n int secPass = n % 8;\n int res = 0;\n while(completePass--){\n res += (8 * press);\n press++;\n }\n res += (secPass * press) ;\n return res;\n }\n};",
"memory": "8000"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> arr(26, 0);\n \n\n for(int i=0;i<word.size();i++){\n int temp = word[i]- 'a';\n arr[temp]++;\n }\n sort(arr.begin(), arr.end());\n int ans=0, cnt=0, mul=1;\n \n for(int i=25;i>=0 && arr[i] != 0;i--){\n ans += arr[i] * mul;\n cnt++;\n if(cnt == 8) {\n cnt = 0;\n mul++;\n }\n }\n return ans;\n }\n};",
"memory": "8300"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n std::vector<int> mp(26, 0);\n\nfor (auto character : word)\n{\n\tmp[character - 'a']++;\n}\n\nint result = 0;\nsort(begin(mp), end(mp), greater<>());\nfor (int i = 0; i < 26; i++)\n{\n\tint freq = mp[i];\n\tif (freq)\n\t{\n\t\tresult += freq * ((i / 8) + 1);\n\t}\n}\n\nreturn result;\n }\n};",
"memory": "8400"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> freq(26, 0);\n for(auto c: word) freq[c - 'a']++;\n sort(freq.begin(), freq.end(), greater<int>());\n int ans = 0;\n for(int i = 0; i < 26; i++){\n ans += freq[i]*((i/8) + 1);\n }\n return ans;\n}\n};",
"memory": "8500"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n \n vector<int> vec(26,0);\n int ans=0;\n for(int i=0;i<word.length();i++)\n {\n vec[word[i]-'a']++;\n }\n sort(vec.begin(),vec.end(),greater<int>());\n for(int i=0;i<26;i++)\n {\n ans+=vec[i]*((i/8)+1);\n }\n \n return ans;\n \n \n }\n};",
"memory": "8500"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n // void pr(vector<int>& arr){\n // cout<<\"[\";\n // for(auto &v:arr){\n // cout<<v<<\" \";\n // }\n // cout<<\"]\"<<\"\\n\";\n // }\n int minimumPushes(string word) {\n vector<int> arr;\n arr.assign(26,0);\n for(auto &ch:word){\n arr[ch-'a']+=1;\n }\n sort(arr.begin(),arr.end(),greater<int>());\n int cnt=1,ans=0;\n for(int i=0;i<26;i++){\n if(cnt<=8){\n cnt++;\n ans+=(arr[i]*1);\n }else if(cnt>8 && cnt<=16){\n cnt++;\n ans+=(arr[i]*2);\n }else if(cnt>16 && cnt<=24){\n cnt++;\n ans+=(arr[i]*3);\n }else if(cnt>24 && cnt<=26){\n cnt++;\n ans+=(arr[i]*4);\n }\n }\n // pr(arr);\n return ans;\n }\n};",
"memory": "8600"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> vc(26,0);\n for(char c:word)\n {\n vc[c-'a']+=1;\n }\n sort(vc.begin(),vc.end(),greater<int>());\n vector<int> vp(26,4);\n for(int i=0;i<8;++i)\n {\n vp[i]=1;\n vp[i+8]=2;\n vp[i+16]=3;\n }\n int ans=0;\n for(int i=0;i<26;++i)\n {\n ans+=vc[i]*vp[i];\n }\n return ans;\n }\n};",
"memory": "8700"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n set<char> st;\n for(char c:word){\n st.insert(c);\n }\n return solve(st.size());\n }\n\n int solve(int n){\n if(n<=8)\n return n;\n else if(n<=16){\n int count=0;\n n=n-8;\n count+=8;\n count+=2*n; \n return count;\n }\n else if(n<=24){\n int count=0;\n n=n-8;\n count+=8;\n count+=16;\n n=n-8;\n count+=n*3;\n return count;\n }\n else{\n int count=0;\n n=n-8;\n count+=8;\n count+=16;\n n=n-8;\n count+=24;\n n=n-8;\n count+=n*4;\n return count; \n }\n return 0;\n }\n};",
"memory": "8800"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n set<char> occur;\n for (char c : word) {\n occur.insert(c);\n }\n int n = occur.size();\n if (n <= 8) return n;\n if (n <= 16) return 2*n-8;\n if (n <= 24) return 3*n-24;\n return 4*n-48;\n }\n};",
"memory": "8900"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n set<char> s;\n for(char ch:word){\n s.insert(ch);\n }\n\n int n = s.size();\n int presses = 0;\n int order = 1;\n\n while(n >0){\n if(n>=8){\n presses += 8*order;\n order++;\n n -= 8;\n }\n else{\n presses += n*order;\n n = 0;\n }\n }\n\n return presses;\n }\n};",
"memory": "9000"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void findInVec(std::vector<int> &indexes, std::vector<int> &values, std::vector<int>::iterator &itV, int x){\n\n itV = std::find(indexes.begin(), indexes.end(), x);\n //std::cout << \"found \" << (char)x << std::endl;\n if(itV != indexes.end()){\n values[distance(indexes.begin(), itV)] += 1;\n //std::cout << x << \" \" << values[distance(indexes.begin(), itV)] << std::endl;\n }\n else{\n indexes.push_back(x);\n values.push_back(1);\n\n //std::cout << indexes[indexes.size()-1] << \" \" << values[indexes.size()-1] << std::endl;\n }\n }\n int minimumPushes(string word) {\n std::vector<int> indexes;\n indexes.reserve(word.size());\n std::vector<int> values;\n values.reserve(word.size());\n\n std::vector<int>::iterator itV;\n\n std::string::iterator itS;\n for(itS = word.begin(); itS != word.end(); ++itS){\n findInVec(indexes, values, itV, (int)*itS);\n }\n std::sort(values.begin(), values.end(), std::greater<>());\n int counter = 0;\n //std::cout << \"counter step\" << std::endl;\n for(int i = 0; i < values.size(); ++i){\n //std::cout << \"value \" << values[i] << \" multi \"<< (i/8+1) << std::endl;\n counter += values[i] * (i/8+1);\n }\n return counter;\n }\n};",
"memory": "9100"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution\n{\npublic:\n int minimumPushes(string word)\n {\n int n = word.size();\n int count = 0, r = 0;\n set<char> s;\n vector<int> v;\n if (n <= 8)\n {\n return n;\n }\n else\n {\n for (char w : word)\n {\n count = 0;\n if (s.find(w) == s.end())\n {\n s.insert(w);\n for (int i = 0; i < n; i++)\n {\n if (word[i] == w)\n {\n count = count + 1;\n }\n }\n }\n v.push_back(count);\n }\n }\n for (int i = 0; i < v.size(); i++)\n {\n if (i < 8)\n {\n r += v[i];\n }\n else if (i >= 8 && i < 16)\n {\n r += 2 * v[i];\n }\n else if (i >= 16 && i < 24)\n {\n r += 3 * v[i];\n }\n else\n {\n r += 4 * v[i];\n }\n }\n return r;\n }\n};",
"memory": "9200"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution\n{\npublic:\n int minimumPushes(string word)\n {\n int n = word.size();\n int count = 0, r = 0;\n set<char> s;\n vector<int> v;\n if (n <= 8)\n {\n return n;\n }\n else\n {\n for (char w : word)\n {\n count = 0;\n if (s.find(w) == s.end())\n {\n s.insert(w);\n for (int i = 0; i < n; i++)\n {\n if (word[i] == w)\n {\n count = count + 1;\n }\n }\n v.push_back(count);\n }\n }\n }\n sort(v.begin(),v.end(),greater<int>());\n for (int i = 0; i < v.size(); i++)\n {\n if (i < 8)\n {\n r += v[i];\n }\n else if (i >= 8 && i < 16)\n {\n r += 2 * v[i];\n }\n else if (i >= 16 && i < 24)\n {\n r += 3 * v[i];\n }\n else\n {\n r += 4 * v[i];\n }\n }\n return r;\n }\n};",
"memory": "9200"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution\n{\npublic:\n int minimumPushes(string word)\n {\n int n = word.size();\n int count = 0, r = 0;\n set<char> s;\n vector<int> v;\n if (n <= 8)\n {\n return n;\n }\n else\n {\n for (char w : word)\n {\n count = 0;\n if (s.find(w) == s.end())\n {\n s.insert(w);\n for (int i = 0; i < n; i++)\n {\n if (word[i] == w)\n {\n count = count + 1;\n }\n }\n }\n v.push_back(count);\n }\n }\n sort(v.begin(),v.end(),greater<int>());\n for (int i = 0; i < v.size(); i++)\n {\n if (i < 8)\n {\n r += v[i];\n }\n else if (i >= 8 && i < 16)\n {\n r += 2 * v[i];\n }\n else if (i >= 16 && i < 24)\n {\n r += 3 * v[i];\n }\n else\n {\n r += 4 * v[i];\n }\n }\n return r;\n }\n};",
"memory": "9300"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int result = 0;\n unordered_map<int, int>mp;\n\n int assign_key = 2;\n\n for(char ch:word)\n {\n if(assign_key>9)\n assign_key = 2;\n\n mp[assign_key]++;\n result = result + mp[assign_key];\n\n assign_key++;\n }\n return result;\n }\n};",
"memory": "9400"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int assign_key = 2;\n int res = 0;\n\n unordered_map<int,int> mpp;\n\n for(char &ch: word){\n if(assign_key>9){\n assign_key = 2;\n }\n mpp[assign_key]++;\n res+=mpp[assign_key];\n assign_key++;\n }\n\n return res;\n }\n};",
"memory": "9500"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int result=0;\n unordered_map<int ,int>mp;\n int assignkey=2;\n for(char &ch:word)\n {\n if(assignkey>9)\n {\n assignkey=2;\n }\n mp[assignkey]++;\n result=result+mp[assignkey];\n assignkey++;\n }\n return result;\n }\n};",
"memory": "9600"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n if(word.size()<=8)\n return word.size();\n unordered_map<char,int>mp;\n for(auto it:word)\n {\n mp[it]++;\n }\n vector<int>v;\n for(auto it:mp)\n {\n v.push_back(it.second);\n }\n sort(v.begin(),v.end(),greater<int>());\n\n int cnt=1;\n int push=1;\n int ans=0;\n\n for(auto it:v)\n {\n if(cnt>8)\n {\n push++;\n cnt=1;\n }\n \n ans+=it*push;\n cnt++;\n }\n return ans;\n \n\n }\n};",
"memory": "9700"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n vector<int> f(26,0);\n for(auto&c:word){\n f[c-'a']++;\n }\n ranges::sort(f);\n int ans = 0;\n priority_queue<int,vector<int>, greater<>> pq;\n for(int i=0; i<8; i++){\n pq.push(1);\n }\n for(int i=25; i>=0; i--){\n ans += pq.top()*f[i];\n int u = pq.top();\n pq.pop();\n pq.push(u+1);\n }\n return ans;\n }\n};",
"memory": "9800"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n\n unordered_map<char, int>mapping;\n //make count of each char in the given string;\n for(auto ch:word){\n mapping[ch]++;\n }\n\n //push the count of every char;\n vector<int> v;\n for(auto p:mapping){\n v.push_back(p.second);\n }\n\n //sort the count array in descending order;\n sort(v.begin(), v.end(), greater<int>());\n int ans=0, ct=0;\n\n //place the first 8 chars in the first place having 1 tap;\n //place the next 8 chars in the second place having 2 tap;\n //place the last 8+ chars in the third place having 3 tap;\n for(auto p:v){\n ct++;\n if(ct<=8){\n ans+=1*p;\n }\n else if(ct<=16){\n ans+=2*p;\n }\n else if(ct<=24){\n ans+=3*p;\n }\n else{\n ans+=4*p;\n }\n }\n return ans;\n }\n};",
"memory": "9900"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n unordered_map<char, int> mp;\n int res = 0;\n \n for(char ch : word) {\n if(mp.size() / 8 >= 1) {\n mp[ch] = (mp.size() / 8) + 1;\n }\n else if(mp[ch] == 0) {\n mp[ch]++;\n }\n res += mp[ch];\n }\n\n return res;\n }\n};",
"memory": "10000"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n //This map will hold all values and their cost.\n unordered_map<char, int> values;\n \n //This array will hold the frequency of each value\n vector<int> freq_vec(26, 0);\n\n //If the costCount variable reaches a certain threshold the cost increases\n int costCount = 2;\n\n //This loop will populate the frequency array\n for (char c : word) {\n freq_vec[c - 'a']++;\n }\n\n //This loop will assing a cost to each value in the string\n for (int i = 0; i < word.size(); ++i) {\n if (costCount >= 2 && costCount <= 9) {\n if (values.count(word[i]) == 0) {\n values[word[i]] = 1;\n costCount++;\n }\n \n } else if (costCount >= 10 && costCount <= 17) {\n if (values.count(word[i]) == 0) {\n values[word[i]] = 2;\n costCount++;\n }\n } else if (costCount >= 18 && costCount <= 25) {\n if (values.count(word[i]) == 0) {\n values[word[i]] = 3;\n costCount++;\n }\n } else {\n if (values.count(word[i]) == 0) {\n values[word[i]] = 4;\n costCount++;\n }\n }\n }\n int cost = 0;\n for (int i = 0; i < freq_vec.size(); ++i) {\n if (freq_vec[i] > 0) {\n cost += values['a' + i] * freq_vec[i];\n }\n }\n return cost;\n }\n};",
"memory": "10100"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\nunordered_map<char, int> mp;\nfor(auto i:word)\nmp[i]++;\npriority_queue<int> pq;\nfor(auto i:mp)\npq.push(i.second);\nint answer=0;\nint counter=0;\nfor(int i=0; i<8 and pq.empty()==false; i++){\n answer+=pq.top();\n pq.pop();\n} \nfor(int i=0; i<8 and pq.empty()==false; i++){\n answer+=2*pq.top();\n pq.pop();\n}\nfor(int i=0; i<8 and pq.empty()==false; i++){\n answer+=pq.top()*3;\n pq.pop();\n}\nfor(int i=0; i<2 and pq.empty()==false; i++){\n answer+=pq.top()*4;\n pq.pop();\n}\n\nreturn answer;\n }\n};",
"memory": "10200"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n int n = word.size();\n int ans = 0;\n vector<int> freq(26,0);\n\n for(auto ele : word) {\n freq[ele-'a']++;\n }\n sort(freq.rbegin(),freq.rend());\n int c = 0;\n for(int i=0;i<26;i++) {\n if(c < 8) {\n ans += freq[i];\n }\n else if(c < 16) {\n ans += (2*freq[i]);\n }\n else if(c < 24) {\n ans += (3*freq[i]);\n }\n else {\n ans += (4*freq[i]);\n }\n c++;\n }\n return ans;\n }\n};",
"memory": "10300"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string word) {\n unordered_map<char, int> mp;\n\n for(int i = 0; i<word.size(); i++){\n mp[word[i]]++;\n }\n\n priority_queue<pair<int, char>> pq;\n \n for(auto i: mp){\n pq.push({i.second, i.first});\n }\n\n int minCount = 0;\n int ans=0;\n while (!pq.empty()){\n ans+=pq.top().first*(minCount/8+1);\n pq.pop();\n minCount++;\n }\n return ans;\n }\n};",
"memory": "10400"
} |
3,275 | <p>You are given a string <code>word</code> containing <strong>distinct</strong> lowercase English letters.</p>
<p>Telephone keypads have keys mapped with <strong>distinct</strong> collections of lowercase English letters, which can be used to form words by pushing them. For example, the key <code>2</code> is mapped with <code>["a","b","c"]</code>, we need to push the key one time to type <code>"a"</code>, two times to type <code>"b"</code>, and three times to type <code>"c"</code> <em>.</em></p>
<p>It is allowed to remap the keys numbered <code>2</code> to <code>9</code> to <strong>distinct</strong> collections of letters. The keys can be remapped to <strong>any</strong> amount of letters, but each letter <strong>must</strong> be mapped to <strong>exactly</strong> one key. You need to find the <strong>minimum</strong> number of times the keys will be pushed to type the string <code>word</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of pushes needed to type </em><code>word</code> <em>after remapping the keys</em>.</p>
<p>An example mapping of letters to keys on a telephone keypad is given below. Note that <code>1</code>, <code>*</code>, <code>#</code>, and <code>0</code> do <strong>not</strong> map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png" style="width: 329px; height: 313px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "abcde"
<strong>Output:</strong> 5
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png" style="width: 329px; height: 313px;" />
<pre>
<strong>Input:</strong> word = "xycdefghij"
<strong>Output:</strong> 12
<strong>Explanation:</strong> The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word.length <= 26</code></li>
<li><code>word</code> consists of lowercase English letters.</li>
<li>All letters in <code>word</code> are distinct.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumPushes(string w) {\n vector<int>v(26,0);\n for(auto a:w){\n ++v[a-'a'];\n }\n sort(v.rbegin(),v.rend());\n int s=0;\n for(int i=0;i<v.size();++i){\n if(v[i]==0)\n return s;\n if(i>23){\n s=s+v[i]*4;\n } else if(i>15){\n s=s+v[i]*3;\n } else if(i>7){\n s=s+v[i]*2;\n } else\n s=s+v[i];\n }\n return s;\n }\n};",
"memory": "10500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n sort(points.begin(), points.end(), [&](vector<int>& a, vector<int>& b) {\n return a[0] < b[0];\n });\n\n int ans = 1, left = points[0][0];\n for (int i = 1; i < points.size(); i++) {\n while (i < points.size() && left + w >= points[i][0]) {\n i++;\n }\n\n if (i != points.size()) {\n ans++;\n left = points[i][0];\n }\n }\n\n return ans;\n }\n};",
"memory": "118400"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n sort(points.begin(), points.end());\n int end = points.front()[0] + w;\n int ans = 0;\n for (auto& x : points)\n if (x[0] > end)\n ans++,end = x[0] + w;\n return ans + 1;\n }\n};",
"memory": "118500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n sort(points.begin(),points.end());\n int n=points.size();\n int ans=0;\n int start=0;\n for(int i=1;i<n;i++)\n {\n if(points[i][0]-points[start][0]>w)\n {\n ans++;\n start=i;\n }\n }\n return ++ans;\n }\n};",
"memory": "118600"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(),points.end());\n int cnt=1,n=points.size();\n int end=points[0][0];\n for(int i=1;i<points.size();i++)\n {\n if(points[i][0]-end<=w) continue;\n else\n {\n cnt++;\n end=points[i][0];\n }\n }\n return cnt;\n }\n};",
"memory": "118700"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) \n {\n std::sort(points.begin() , points.end());\n int x = points[0][0];\n int ans = 0;\n for(int i = 1 ; i < points.size() + 1; i++)\n {\n if(i == points.size())\n {\n ans++;\n break;\n }\n if(std::abs(x - points[i][0]) > w)\n {\n ans++;\n x = points[i][0];\n }\n }\n return ans;\n }\n};",
"memory": "118800"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n int n = points.size();\n int count=0;\n int end =-1;\n sort(points.begin(), points.end());\n for(int i=0;i<n;i++){\n int start = points[i][0];\n if(w==0 && start!=0){\n return n;\n }\n if(start > end){\n count++;\n end = start+w;\n }\n }\n return count;\n }\n}; ",
"memory": "118900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n // Sort points by their x-coordinate\n sort(points.begin(), points.end());\n int n = points.size();\n // Initial placement of the first rectangle\n int rectangles = 1;\n int current_end = points[0][0] + w;\n for (int i = 1; i < n; ++i) {\n // If the current point is out of the coverage of the current rectangle, place a new one\n if (points[i][0] > current_end) {\n rectangles++;\n current_end = points[i][0] + w;\n }\n }\n return rectangles;\n }\n};",
"memory": "119000"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(), points.end());\n int c{points[0][0]+w};\n int retVal{1};\n for(const auto& v: points){\n if(v[0] > c){\n retVal++;\n c = v[0]+w;\n }\n }\n return retVal;\n }\n};",
"memory": "119100"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& point, int w) {\n int n = point.size();\n sort(point.begin(),point.end());\n int count=0;\n int prev = point[0][0];\n for(int i=1;i<n;i++){\n if(point[i][0]-prev<=w){\n continue;\n }\n else{\n prev = point[i][0];\n count++;\n }\n }\n return count+1;\n }\n};",
"memory": "119200"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n sort(points.begin(),points.end(),[](vector<int>&a,vector<int>&b){return a[0]>b[0];});\n\n int ans=0;\n int n=points.size();\n vector<bool> vis(n,false);\n for(int i=0;i<n;i++){\n if(vis[i]) continue;\n int nrec =points[i][0];\n ans++;\n\n for(int j=i;j<n;j++){\n if(points[j][0]>=nrec-w) vis[j]=true;\n else break;\n } \n }\n return ans;\n }\n};",
"memory": "119500"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n // sort(points.begin(),points.end(),[&](vector<int> a,vector<int> b) {\n // return a[0] < b[0];\n // });\n vector<int> X(points.size());\n for(int i = 0;i < points.size();i++) {\n X[i] = points[i][00];\n }\n sort(X.begin(),X.end());\n int prev = X[0];\n int ans = 1;\n for(int i = 1;i < points.size();i++) {\n if(X[i] - prev <= w) continue;\n else {\n ans++;\n prev = X[i];\n }\n }\n return ans;\n }\n};",
"memory": "119900"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n ios_base :: sync_with_stdio(false);\n cin.tie(nullptr);cout.tie(nullptr);\n int ans = 0;\n int start_x = -2;\n vector<int> dau_x(points.size());\n for (int i=0;i<points.size();i++) dau_x[i] = points[i][0];\n\n sort(dau_x.begin(),dau_x.end());\n\n for (int arr:dau_x){\n if (start_x<arr){\n start_x = arr+w;\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "120000"
} |
3,390 | <p>You are given a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer <code>w</code>. Your task is to <strong>cover</strong> <strong>all</strong> the given points with rectangles.</p>
<p>Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> <strong>must</strong> be satisfied for each rectangle.</p>
<p>A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.</p>
<p>Return an integer denoting the <strong>minimum</strong> number of rectangles needed so that each point is covered by <strong>at least one</strong> rectangle<em>.</em></p>
<p><strong>Note:</strong> A point may be covered by more than one rectangle.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png" style="width: 205px; height: 300px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(2, 8)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(4, 8)</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png" style="width: 260px; height: 250px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">3</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(0, 0)</code> and its upper end at <code>(2, 2)</code></li>
<li>A rectangle with a lower end at <code>(3, 0)</code> and its upper end at <code>(5, 5)</code></li>
<li>A rectangle with a lower end at <code>(6, 0)</code> and its upper end at <code>(6, 6)</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png" style="height: 150px; width: 127px;" /></p>
<div class="example-block" style="
border-color: var(--border-tertiary);
border-left-width: 2px;
color: var(--text-secondary);
font-size: .875rem;
margin-bottom: 1rem;
margin-top: 1rem;
overflow: visible;
padding-left: 1rem;
">
<p><strong>Input:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">points = [[2,3],[1,2]], w = 0</span></p>
<p><strong>Output:</strong> <span class="example-io" style="
font-family: Menlo,sans-serif;
font-size: 0.85rem;
">2</span></p>
<p><strong>Explanation: </strong></p>
<p>The image above shows one possible placement of rectangles to cover the points:</p>
<ul>
<li>A rectangle with a lower end at <code>(1, 0)</code> and its upper end at <code>(1, 2)</code></li>
<li>A rectangle with a lower end at <code>(2, 0)</code> and its upper end at <code>(2, 3)</code></li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code></li>
<li><code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code></li>
<li><code>0 <= w <= 10<sup>9</sup></code></li>
<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {\n ios_base :: sync_with_stdio(false);\n cin.tie(nullptr);cout.tie(nullptr);\n int ans = 0;\n int start_x = -2;\n vector<int> dau_x(points.size());\n for (int i=0;i<points.size();i++) dau_x[i] = points[i][0];\n\n sort(dau_x.begin(),dau_x.end());\n\n for (int arr:dau_x){\n if (start_x<arr){\n start_x = arr+w;\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "120100"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.