classSolution: defmaxResult(self, nums: List[int], k: int) -> int: if (size := len(nums)) == 1: return nums[0] if k == 1: returnsum(nums) score = nums[0] priority_queue = [] # since heapq is a min-heap, # we use negative of the numbers to mimic a max-heap heapq.heappush(priority_queue, (-nums[0], 0)) for i inrange(1, size):
# pop the old index while priority_queue[0][1] < i - k: heapq.heappop(priority_queue) score = nums[i] - priority_queue[0][0] heapq.heappush(priority_queue, (-score, i)) return score