Home >Backend Development >PHP Tutorial >Take Gifts From the Richest Pile
2558. Take Gifts From the Richest Pile
Difficulty: Easy
Topics: Array, Heap (Priority Queue), Simulation
You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:
Return the number of gifts remaining after k seconds.
Example 1:
Example 2:
Constraints:
Hint:
Solution:
We can utilize a max-heap (priority queue) since we need to repeatedly pick the pile with the maximum number of gifts. A max-heap will allow us to efficiently access the largest pile in constant time and update the heap after taking gifts from the pile.
Use a Max-Heap:
Process Each Second:
Termination:
Let's implement this solution in PHP: 2558. Take Gifts From the Richest Pile
<?php /** * @param Integer[] $gifts * @param Integer $k * @return Integer */ function pickGifts($gifts, $k) { ... ... ... /** * go to ./solution.php */ } // Example usage: $gifts1 = [25, 64, 9, 4, 100]; $k1 = 4; echo pickGifts($gifts1, $k1) . "\n"; // Output: 29 $gifts2 = [1, 1, 1, 1]; $k2 = 4; echo pickGifts($gifts2, $k2) . "\n"; // Output: 4 ?> <h3> Explanation: </h3> <ol> <li> <p><strong>Heap Initialization</strong>:</p> <ul> <li> SplPriorityQueue is used to simulate a max-heap. The insert method allows us to push elements into the heap with a priority.</li> </ul> </li> <li> <p><strong>Processing the Largest Pile</strong>:</p> <ul> <li>For k iterations, the largest pile is extracted using extract.</li> <li>The number of gifts left behind is calculated as the floor of the square root of the current largest pile using floor(sqrt(...)).</li> <li>The reduced pile is re-inserted into the heap.</li> </ul> </li> <li> <p><strong>Summing Remaining Gifts</strong>:</p> <ul> <li>After the k operations, all elements in the heap are extracted and summed up to get the total number of remaining gifts.</li> </ul> </li> <li> <p><strong>Edge Cases</strong>:</p> <ul> <li>If gifts is empty, the result is 0.</li> <li>If k is larger than the number of operations possible, the algorithm gracefully handles it.</li> </ul> </li> </ol> <h3> Time Complexity: </h3> <ul> <li> <strong>Heap operations (insert and extract)</strong>: Each heap operation (insertion and extraction) takes <em><strong>O(log n)</strong></em>, where <em><strong>n</strong></em> is the number of piles.</li> <li> <strong>Looping through k operations</strong>: We perform <em><strong>k</strong></em> operations, each involving heap extraction and insertion, both taking <em><strong>O(log n)</strong></em>.</li> </ul> <p>Thus, the total time complexity is <em><strong>O(k log n)</strong></em>, where <em><strong>n</strong></em> is the number of piles and <em><strong>k</strong></em> is the number of seconds.</p> <h3> Example Walkthrough: </h3> <p>For the input:<br> </p> <pre class="brush:php;toolbar:false"><?php /** * @param Integer[] $gifts * @param Integer $k * @return Integer */ function pickGifts($gifts, $k) { ... ... ... /** * go to ./solution.php */ } // Example usage: $gifts1 = [25, 64, 9, 4, 100]; $k1 = 4; echo pickGifts($gifts1, $k1) . "\n"; // Output: 29 $gifts2 = [1, 1, 1, 1]; $k2 = 4; echo pickGifts($gifts2, $k2) . "\n"; // Output: 4 ?>
The sum of the remaining gifts is 5 8 9 4 3 = 29.
This approach efficiently solves the problem using a max-heap and performs well within the given constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Take Gifts From the Richest Pile. For more information, please follow other related articles on the PHP Chinese website!