题解 | #生成窗口最大值数组#

生成窗口最大值数组

http://www.nowcoder.com/practice/b316c7f9617744b98fa311ae29ac516c

n, k = map(int, input().split())
nums = list(map(int, input().split()))
import collections
stack = collections.deque()
res = []
for i in range(k):
    while stack and stack[-1] < nums[i]:
        stack.pop()
    stack.append(nums[i])
res.append(stack[0])
for i in range(k, n):
    if nums[i - k] == stack[0]:
        stack.popleft()
    while stack and stack[-1] < nums[i]:
        stack.pop()
    stack.append(nums[i])
    res.append(stack[0])
print(*res)
    
全部评论

相关推荐

点赞 评论 收藏
分享
求面试求offer啊啊啊啊:1600一个月?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务