{"id":9335,"date":"2021-12-31T14:20:09","date_gmt":"2021-12-31T22:20:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9335"},"modified":"2021-12-31T14:22:57","modified_gmt":"2021-12-31T22:22:57","slug":"leetcode-1962-remove-stones-to-minimize-the-total","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1962-remove-stones-to-minimize-the-total\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1962. Remove Stones to Minimize the Total"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>piles<\/code>, where&nbsp;<code>piles[i]<\/code>&nbsp;represents the number of stones in the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;pile, and an integer&nbsp;<code>k<\/code>. You should apply the following operation&nbsp;<strong>exactly<\/strong>&nbsp;<code>k<\/code>&nbsp;times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose any&nbsp;<code>piles[i]<\/code>&nbsp;and&nbsp;<strong>remove<\/strong>&nbsp;<code>floor(piles[i] \/ 2)<\/code>&nbsp;stones from it.<\/li><\/ul>\n\n\n\n<p><strong>Notice<\/strong>&nbsp;that you can apply the operation on the&nbsp;<strong>same<\/strong>&nbsp;pile more than once.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;possible total number of stones remaining after applying the&nbsp;<\/em><code>k<\/code><em>&nbsp;operations<\/em>.<\/p>\n\n\n\n<p><code>floor(x)<\/code>&nbsp;is the&nbsp;<strong>greatest<\/strong>&nbsp;integer that is&nbsp;<strong>smaller<\/strong>&nbsp;than or&nbsp;<strong>equal<\/strong>&nbsp;to&nbsp;<code>x<\/code>&nbsp;(i.e., rounds&nbsp;<code>x<\/code>&nbsp;down).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> piles = [5,4,9], k = 2\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong>&nbsp;Steps of a possible scenario are:\n- Apply the operation on pile 2. The resulting piles are [5,4,5].\n- Apply the operation on pile 0. The resulting piles are [3,4,5].\nThe total number of stones in [3,4,5] is 12.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> piles = [4,3,6,7], k = 3\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong>&nbsp;Steps of a possible scenario are:\n- Apply the operation on pile 2. The resulting piles are [4,3,3,7].\n- Apply the operation on pile 3. The resulting piles are [4,3,3,4].\n- Apply the operation on pile 0. The resulting piles are [2,3,3,4].\nThe total number of stones in [2,3,3,4] is 12.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= piles.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= piles[i] &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy \/ Heap<\/strong><\/h2>\n\n\n\n<p>Always choose the largest pile to remove.<\/p>\n\n\n\n<p>Time complexity: O(n + klogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minStoneSum(vector<int>& piles, int k) {\n    int total = accumulate(begin(piles), end(piles), 0);\n    priority_queue<int> q(begin(piles), end(piles));\n    while (k--) {\n      int curr = q.top(); q.pop();\n      int remove = curr \/ 2;\n      total -= remove;\n      q.push(curr - remove);\n    }\n    return total;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;piles, where&nbsp;piles[i]&nbsp;represents the number of stones in the&nbsp;ith&nbsp;pile, and an integer&nbsp;k. You should apply the following operation&nbsp;exactly&nbsp;k&nbsp;times: Choose any&nbsp;piles[i]&nbsp;and&nbsp;remove&nbsp;floor(piles[i] \/ 2)&nbsp;stones&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,73,177,382],"class_list":["post-9335","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-heap","tag-medium","tag-priority_queue","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9335"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9335\/revisions"}],"predecessor-version":[{"id":9338,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9335\/revisions\/9338"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}