We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent eee9662 commit 4a4d26eCopy full SHA for 4a4d26e
data_structure/stack_queue.md
@@ -287,10 +287,24 @@ class Solution:
287
> 返回 滑动窗口中的最大值 。
288
思路:这道题得想到一个数据结构,当滑动窗口一进一出,能够自动把最大值拱上来,只有最大堆了
289
```python
290
-
+import heapq
291
+class Solution:
292
+ def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: #最大堆队列
293
+ hq = list()
294
+ ans = list()
295
+ for i, num in enumerate(nums):
296
+ while hq and hq[0][1] <= i - k:
297
+ heapq.heappop(hq)
298
+ heapq.heappush(hq, [-num,i])
299
+ if i >= k - 1:
300
+ ans.append(-hq[0][0])
301
+ return ans
302
303
```
304
305
+或者考虑使用双边队列
306
+
307
308
309
310
### [decode-string](https://leetcode-cn.com/problems/decode-string/)
0 commit comments