From fe1eaeff4e10f6533a41539bf0e99f32021f1cc3 Mon Sep 17 00:00:00 2001 From: freshield Date: Tue, 11 Oct 2022 19:56:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0python3=E7=9A=84=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=EF=BC=8C=E6=9B=BF=E6=8D=A2list=E4=B8=BAdeque=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=94=A8list=E4=BC=9A=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 3f402f12..b86fdd15 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -296,16 +296,19 @@ class Solution { ``` Python: -```python +```python3 +from collections import deque + + class MyQueue: #单调队列(从大到小 def __init__(self): - self.queue = [] #使用list来实现单调队列 + self.queue = deque() #这里需要使用deque实现单调队列,直接使用list会超时 #每次弹出的时候,比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。 #同时pop之前判断队列当前是否为空。 def pop(self, value): if self.queue and value == self.queue[0]: - self.queue.pop(0)#list.pop()时间复杂度为O(n),这里可以使用collections.deque() + self.queue.popleft()#list.pop()时间复杂度为O(n),这里需要使用collections.deque() #如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。 #这样就保持了队列里的数值是单调从大到小的了。 From c4a12f7dc4ac6dccd16c1c81d06f4404311658e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 16 Oct 2022 10:46:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index b86fdd15..d41c092d 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -296,7 +296,7 @@ class Solution { ``` Python: -```python3 +```python from collections import deque