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:
Input: gifts = [25,64,9,4,100], k = 4
Output: 29Explanation :
The gifts are taken in the following way:
Example 2:
Input: gifts = [1,1,1,1], k = 4
Output: 4Constraints:
1 <= gifts.length <= 10001 <= gifts[i] <= 1,000,000,0001 <= k <= 1000class Solution:
def pickGifts(self, gifts: List[int], k: int) -> int:
for _ in range(k):
maxIdx = 0
for i in range(1, len(gifts)):
if gifts[i] > gifts[maxIdx]:
maxIdx = i
gifts[maxIdx] = int(sqrt(gifts[maxIdx]))
return sum(gifts)Where is the size of input array, is the number of seconds.
class Solution:
def pickGifts(self, gifts: List[int], k: int) -> int:
for i in range(len(gifts)):
gifts[i] = -gifts[i]
heapq.heapify(gifts)
for _ in range(k):
n = -heapq.heappop(gifts)
heapq.heappush(gifts, -floor(sqrt(n)))
return -sum(gifts)Where is the size of input array, is the number of seconds.