<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>max heap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/max-heap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/max-heap/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2023 05:15:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>max heap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/max-heap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2558. Take Gifts From the Richest Pile</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Feb 2023 05:12:41 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[max heap]]></category>
		<category><![CDATA[priority queue]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9923</guid>

					<description><![CDATA[<p>You are given an integer array&#160;gifts&#160;denoting the number of gifts in various piles. Every second, you do the following: Choose the pile with the maximum&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/">花花酱 LeetCode 2558. Take Gifts From the Richest Pile</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an integer array&nbsp;<code>gifts</code>&nbsp;denoting the number of gifts in various piles. Every second, you do the following:</p>



<ul><li>Choose the pile with the maximum number of gifts.</li><li>If there is more than one pile with the maximum number of gifts, choose any.</li><li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li></ul>



<p>Return&nbsp;<em>the number of gifts remaining after&nbsp;</em><code>k</code><em>&nbsp;seconds.</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
<strong>Output:</strong> 29
<strong>Explanation:</strong> 
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> gifts = [1,1,1,1], k = 4
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. 
That is, you can't take any pile with you. 
So, the total gifts remaining are 4.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= gifts.length &lt;= 10<sup>3</sup></code></li><li><code>1 &lt;= gifts[i] &lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= k &lt;= 10<sup>3</sup></code></li></ul>



<h2><strong>Solution: Priority Queue</strong></h2>



<p>Keep all numbers in a priority queue (max heap), each time extract the top one (largest one), then put num  &#8211; sqrt(num) back to the queue.</p>



<p>Tip: We can early return if all the numbers become 1.</p>



<p>Time complexity: O(n + klogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  long long pickGifts(vector&lt;int&gt;&amp; gifts, int k) {
    long long ans = accumulate(begin(gifts), end(gifts), 0LL);
    priority_queue&lt;int&gt; q(begin(gifts), end(gifts));
    while (k-- &amp;&amp; q.top() &gt; 1) {
      int cur = q.top(); q.pop();
      int next = sqrt(cur);
      ans -= (cur - next);
      q.push(next);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/">花花酱 LeetCode 2558. Take Gifts From the Richest Pile</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
