Leetcode Problem 2558. Take Gifts From the Richest Pile

2558. Take Gifts From the Richest Pile

Leetcode Solutions

Priority Queue Approach

  1. Initialize a priority queue and add all elements from the gifts array to it.
  2. Repeat k times: a. Extract the maximum element from the priority queue. b. Calculate the floor of the square root of the extracted element. c. Push the calculated square root back into the priority queue.
  3. After k iterations, sum up all the elements remaining in the priority queue to get the total number of gifts left.
UML Thumbnail

Sorting Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...