classSolution: defpickGifts(self, gifts: List[int], k: int) -> int: for i inrange(len(gifts)): gifts[i] *= -1 heapify(gifts) print(gifts) while k and -gifts[0]>1: heapreplace(gifts,-isqrt(-gifts[0])) k -= 1 return -sum(gifts)
我的方法(时间复杂度被爆杀):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defpickGifts(self, gifts: List[int], k: int) -> int: while(k != 0): temp=0 temp_num = gifts[0] for i inrange(len(gifts)): if gifts[i]>temp_num: temp_num = gifts[i] temp = i
gifts[temp] = int(gifts[temp] **0.5) k = k -1 sum_gifts = 0 for j inrange(len(gifts)): sum_gifts += gifts[j] return sum_gifts