mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			45 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			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>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 <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
 | 
						|
 | 
						|
<p> </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't take any pile with you. 
 | 
						|
So, the total gifts remaining are 4.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= gifts.length <= 10<sup>3</sup></code></li>
 | 
						|
	<li><code>1 <= gifts[i] <= 10<sup>9</sup></code></li>
 | 
						|
	<li><code>1 <= k <= 10<sup>3</sup></code></li>
 | 
						|
</ul>
 |