首页 > 其他分享 >LeetCode 239. Sliding Window Maximum 单调队列

LeetCode 239. Sliding Window Maximum 单调队列

时间:2023-07-28 21:35:40浏览次数:36  
标签:sliding nums 队列 res window Maximum int Window LeetCode

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max sliding window.

Solution

使用一个单调队列来维护。如果当前队尾的元素比当前的小,那么一直将队尾的元素 \(pop\) 出去,然后将此时的下标加入队列。

点击查看代码
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        q, res = deque(), []

        for r in range(len(nums)):
            # remove all the items less than the current one
            while q and nums[q[-1]]<nums[r]:
                q.pop()
            
            q.append(r)

            if r+1<k: # not full 
                continue
            
            if r-q[0]+1>k: # most left index out of window
                q.popleft()
            
            res.append(nums[q[0]])
        
        return res


标签:sliding,nums,队列,res,window,Maximum,int,Window,LeetCode
From: https://www.cnblogs.com/xinyu04/p/17588930.html

相关文章

  • 代码随想录算法训练营第三天| LeetCode 203.移除链表元素(同时也对整个单链表进行增删
    203.移除链表元素      题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html    卡哥题目建议:本题最关键是要理解虚拟头结点的使用技巧,这个对链表题目很重要。   做题思路:   ......
  • 多个账号如何同时远程登陆Windows11(开启home版远程登陆)
    首先去下面链接下载最新版的RDPWrap,然后解压https://github.com/stascorp/rdpwrap/releases 右键以管理员身份运行 install.bat运行完成之后运行RDPConf.exe,如果全绿,那么就没问题了如果遇到像我这个同样的问题,那么就需要重新下载一个rdpwrap.ini 文件并且替换掉C:\Progra......
  • Windows本地IDEA运行mapreduce报错java.io.FileNotFoundException: HADOOP_HOME and h
    问题原因在windows运行hadoopJob程序的时候需要模拟下hadoop的运行环境。否则出现会出现标题的问题。解决方案下载Hadoop的bin目录https://github.com/s911415/apache-hadoop-3.1.3-winutils将步骤1中下载的文件配置成环境变量HADOOP_HOME(指向解压之后的的bin的上级目录)。......
  • How to disable Windows 10 DNS Cache services
    HiAdithya,DisableDNSClientthroughregistry:GotoHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Dnscache,LocatetheStartregistrykeyandchangeitsvaluefrom2(Automatic)to4(Disabled)DisableDNSclientthroughcommandline:REGadd......
  • windows下shellcode注入的例子(WriteProcessMemory+CreateRemoteThread)
    vs里x64编译如下代码:  #include<iostream>#include<Windows.h>//#include"common.h"intmain(){ //msfvenom-pwindows/x64/execCMD=notepad.exe-fc unsignedcharshellcode[]= "\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x0......
  • WindowManager 中ayoutParams的各…
    1.publicintx; 如果忽略gravity属性,那么它表示窗口的绝对X位置。 什么是gravity属性呢?简单地说,就是窗口如何停靠。LEFT或Gravity.RIGHT之后,x值就表示到特定边的距离。2.publicinty; 如果忽略gravity属性,那么它表示窗口的绝对Y位置。当设......
  • Windows环境下安装及部署Nginx
    一、安装Nginx教程1、官网下载地址:https://nginx.org/en/download.html2、下载教程:选择Stable version版本下载到本地3、下载完成后,解压放入本地非中文的文件夹中:4、启动nginx:双击nginx.exe,若双击未弹出内容,则说明端口被占用,请参照第6步 或者使用命令行:输入start nginx......
  • window.location.href的用法 导出数据
    一、前言window.location.href是一个用于获取当前页面URL或让浏览器跳转到新URL的重要方法,是window.location对象的属性。它返回一个字符串,表示当前页面的URL;同时,当通过将URL指定给window.location.href时,可以让浏览器跳转到新的URL。二、常见用例self.location.hre......
  • C++实现Windows释放资源文件到本地
     C++实现释放资源文件到本地用途很多,可以实现动态释放DLL文件,需时加载./***\brief释放资源文件到本地*\paramlpName资源文件名称*\paramlpType资源文件分类*\paramlpFileName生成到本地的文件名称*\return*/BOOLReleaseResource(_In_LPCWSTRlp......
  • Windows OS 下通过VNC访问Linux OS(可视化界面)
    VNC介绍:VNC(VirtualNetworkConsole),即虚拟网络控制台,它是一款基于UNIX和Linux操作系统的优秀远程控制工具软件,由著名的AT&T的欧洲研究实验室开发,远程控制能力强大,高效实用,并且免费开源。VNC基本上是由两部分组成:一部分是客户端的应用程序(vncviewer);另外一部分是服务器......