239.滑动窗口最大值
题目链接: https://leetcode.cn/problems/sliding-window-maximum/
class MyQueue:
def __init__(self):
self.queue = deque()
def push(self, value):
while self.queue and value > self.queue[-1]:
self.queue.pop()
self.queue.append(value)
def pop(self, value):
if self.queue and value == self.queue[0]:
self.queue.popleft()
def front(self):
return self.queue[0]
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
que = MyQueue()
res = []
for i in range(k):
que.push(nums[i])
res.append(que.front())
for i in range(k, len(nums)):
que.pop(nums[i-k])
que.push(nums[i])
res.append(que.front())
return res
347.前K个高频元素
题目链接: https://leetcode.cn/problems/top-k-frequent-elements/
明天补下这道题目 暂时还没搞懂