java.lang.Object
g2501_2600.s2558_take_gifts_from_the_richest_pile.Solution

public class Solution extends Object
2558 - Take Gifts From the Richest Pile.<p>Easy</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> gifts = [25,64,9,4,100], k = 4</p> <p><strong>Output:</strong> 29</p> <p><strong>Explanation:</strong> The gifts are taken in the following way:</p> <ul> <li>In the first second, the last pile is chosen and 10 gifts are left behind.</li> <li>Then the second pile is chosen and 8 gifts are left behind.</li> <li>After that the first pile is chosen and 5 gifts are left behind.</li> <li>Finally, the last pile is chosen again and 3 gifts are left behind.</li> </ul> <p>The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> gifts = [1,1,1,1], k = 4</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.</p> <p>That is, you can&rsquo;t take any pile with you.</p> <p>So, the total gifts remaining are 4.</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • pickGifts

      public long pickGifts(int[] gifts, int k)