1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/从数量最多的堆取走礼物(English) [take-gifts-from-the-richest-pile].html

45 lines
1.7 KiB
HTML

<p>You are given an integer array <code>gifts</code> 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>Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile.</li>
</ul>
<p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<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 class="example">Example 2:</strong></p>
<pre>
<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&#39;t take any pile with you.
So, the total gifts remaining are 4.
</pre>
<p>&nbsp;</p>
<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>