labuladong / basic-challenge

200 stars 22 forks source link

已结束 #354

Closed labuladong closed 1 year ago

labuladong commented 1 year ago

本期打卡已结算完成。报名最新一期的打卡活动 点这里

CarolSSS commented 1 year ago

https://leetcode.com/problems/sliding-window-maximum/solutions/3916163/sliding-window-monotonic-queue/

ljrirene commented 1 year ago

https://leetcode.com/problems/sliding-window-maximum/solutions/3916234/239-sliding-window-maximum/

DannyT70 commented 1 year ago

https://leetcode.cn/problems/sliding-window-maximum/solution/dan-diao-dui-lie-by-sleepy-shawffi-3lp7/

alyssasuper3 commented 1 year ago

https://leetcode.cn/problems/sliding-window-maximum/solutions/2391234/hua-dong-chuang-kou-de-zui-da-zhi-by-aly-9dw6/

KarlZhu-SE commented 1 year ago

https://leetcode.com/problems/sliding-window-maximum/solutions/3916875/sliding-window/

Pikalili commented 1 year ago

https://leetcode.com/problems/sliding-window-maximum/solutions/3916968/topic/

AstrKing commented 1 year ago

239:https://leetcode.cn/problems/sliding-window-maximum/solutions/2065160/hua-dong-chuang-kou-zui-da-zhi-by-aktrki-63mo/

zhiqunyang1021 commented 1 year ago

https://leetcode.cn/problems/sliding-window-maximum/solutions/2391725/she-ji-dui-lie-by-ren-you-yi-xin-inio/

cs-gavin-huang commented 1 year ago

https://leetcode.com/problems/sliding-window-maximum/solutions/3918153/sliding-window-maximum/

tonyzhu163 commented 1 year ago
# Aug 16

# LC 239 Sliding Window Max
# Monotonic queue maintains largest element efficiently
# Base on deque (doubly linked list) pop and append in two directions
# pop all elements smaller than current element

from collections import deque

class M_Queue:
    def __init__(self):
        self.nums = deque()

    def push(self, ele):
        while len(self.nums) > 0 and self.nums[-1] < ele:
            self.nums.pop()
        self.nums.append(ele)

    def pop(self, ele):
        if ele == self.nums[0]:
            self.nums.popleft()

    def max(self):
        return self.nums[0]

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        win = M_Queue()
        res = []

        for idx, i in enumerate(nums):
            if idx < k - 1:
                win.push(i)
            else:
                win.push(i)
                res.append(win.max())
                win.pop(nums[idx - k + 1])

        return res
ElaineZhou-moon commented 1 year ago

https://leetcode.com/problems/sliding-window-maximum/solutions/2223325/topic/

YilinYan0 commented 1 year ago

239

https://leetcode.com/problems/sliding-window-maximum/solutions/3918674/monotonic-queue/