classSolution: defmaxHeap(self,nums:[int],i:int,heap_size:int): while1: l_i=2*i+1 r_i=2*i+2 lst=i if l_i<heap_size and nums[l_i]<nums[lst]: lst=l_i if r_i<heap_size and nums[r_i]<nums[lst]: lst=r_i if lst==i: break nums[i],nums[lst]=nums[lst],nums[i] i=lst defbuildmaxHeap(self,nums:[int],heap_size:int)->None: for i inrange (heap_size//2-1,-1,-1): self.maxHeap(nums,i,heap_size) definventoryManagement(self, stock: List[int], cnt: int) -> List[int]: if cnt == 0: return [] n = len(stock) heap_size = min(cnt, n) self.buildmaxHeap(stock, n) result = [] import copy temp = copy.deepcopy(stock) for i inrange(min(cnt, n)): temp[0], temp[n - i - 1] = temp[n - i - 1], temp[0] result.append(temp[n - i - 1]) self.maxHeap(temp, 0, n - i - 1) return result