Leetcode # 2558. Take Gifts From the Richest Pile
- 2024.12.12
- ★ Easy heqpq LeetCode Priority Queue
Problem
https://leetcode.com/problems/take-gifts-from-the-richest-pile
Solution
Time Complexity: O(len(gifts))
Space Complexity: O(len(gifts))
(The input and output generally do not count towards the space complexity.)
class Solution: def pickGifts(self, gifts: List[int], k: int) -> int: gifts_pq = [-g for g in gifts] heapify(gifts_pq) for i in range(k): max_g = -heappop(gifts_pq) heappush(gifts_pq, -isqrt(max_g)) return -sum(gifts_pq)
Last Updated on 2024/12/15 by A1go