diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 23bf615b..2db1942a 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -339,6 +339,32 @@ class Solution: return result ``` +#### 新解法:用"堆排序"实现 +* 时间复杂度:`O(n * log(n))`, 比`单调队列`解法要慢。 + +```python +import heapq + + +class Solution: + def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: + results = [] + num_index_list = [] # 将用“堆排序”对它进行排序,元素为 (num, index) 元组 + + for i in range(len(nums)): + # 把 nums[i] 值取负数,最大的就到最小,合乎 Python 堆排序从小到大的规则。 + # 还要把 index (i) 存入,因为通过 i 可知道对应的 num 何时不能再被使用(num 已经处在左侧窗口的更左边) + heapq.heappush(num_index_list, (-nums[i], i)) + # num_index_list[0]是最小值所在 tuple;'<= i - k' 表示 num 已经处在左侧窗口的更左边 + while num_index_list[0][1] <= i - k: # while 表示所有过气 num 都要丢弃 + heapq.heappop(num_index_list) # 丢弃最小值 + + if i >= k - 1: + results.append(-num_index_list[0][0]) # 第一个就是最小值,负最小值就是最大值,加入结果集 + + return results +``` + ### Go: ```go