题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param num int整型一维数组
# @param size int整型
# @return int整型一维数组
#
class Solution:
def maxInWindows(self , num: List[int], size: int) -> List[int]:
# write code here
if size == 0: # 不要忘记特判
return []
q = []
res = []
for i, x in enumerate(num):
while len(q) > 0 and num[q[-1]] <= x: # 尾部小于等于当前x,shandiao
q = q[:-1]
if len(q) > 0 and i - size + 1 > q[0]: # 删去超过大小的头部
q = q[1:]
q.append(i) # 加入
if i >= size - 1: # 超了就加入结果队列
res.append(num[q[0]])
return res
算法刷题记录 文章被收录于专栏
刷题,记录牛客的101