Skip to content

Commit 4a4d26e

Browse files
committed
Update stack_queue.md
1 parent eee9662 commit 4a4d26e

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

data_structure/stack_queue.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,24 @@ class Solution:
287287
> 返回 滑动窗口中的最大值 。
288288
思路:这道题得想到一个数据结构,当滑动窗口一进一出,能够自动把最大值拱上来,只有最大堆了
289289
```python
290-
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
291302

292303
```
293304

305+
或者考虑使用双边队列
306+
307+
294308

295309

296310
### [decode-string](https://leetcode-cn.com/problems/decode-string/)

0 commit comments

Comments
 (0)