mycode  89.27%

class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
""" if k == 0 or nums == [] or k > len(nums): return [] length = len(nums)
temp = []
for j in range(0,k):
temp.append(nums[j])
first = max(temp)
flag = first == nums[0]
res = [first]
for i in range(1,length-k+1):
if not flag :
first = max(first,nums[i+k-1])
else:
temp = []
for j in range(0,k):
temp.append(nums[i+j])
first = max(temp)
res.append(first)
flag = first == nums[i]
return res

参考

class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if not nums: return [] # max for the first window
cur_max = max(nums[:k])
res = [cur_max]
left = 0 for right in range(k, len(nums)):
# recalculate the new max and index as the previous max is not in this window
if nums[right] > cur_max or nums[right-k] == cur_max :
cur_max = max(nums[right-k+1: right+1]) res.append(cur_max) return res

leetcode-hard-array-239. Sliding Window Maximum的更多相关文章

  1. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  2. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  3. [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...

  5. [leetcode]239. Sliding Window Maximum滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  6. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  7. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  8. (heap)239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  9. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

  10. [leetcode] #239 Sliding Window Maximum (Hard)

    原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...

随机推荐

  1. Centos7搭建Docker部署LNMP

    1.首先呢先更新yum源 yum update 2.1.安装docker存储库 yum install -y yum-utils \ device-mapper-persistent-data \ l ...

  2. centos redis自启动

    #!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database # Simple R ...

  3. PKGSRC

    PKGSRC简介 pkgsrc: The NetBSD Packages Collection The NetBSD Packages Collection (pkgsrc) 是在NetBSD系统以及 ...

  4. 9月24日开始发布,主打安全的Librem 5 Linux手机

    曾推出搭载PureOS Linux发行版本Librem笔记本系列的硬件厂商Purism,今天正式宣布了Librem 5 Linux手机的最终和官方发售日期.Librem 5于2017年10月正式发布, ...

  5. Galera Cluster 实现mysql的高可用 (Percona XtraDB Cluster)

    Galera Cluster 实现mysql的高可用 (Percona XtraDB Cluster) # 基础搭建 # 设备:三台主机 192.168.47.101 192.168.47.102 1 ...

  6. 转载 如何使用批处理 动态改变path实现改变JDK版本

    http://www.cnblogs.com/xdp-gacl/p/5209386.html 1 @echo off 2 3 rem --- Base Config 配置JDK的安装目录 --- 4 ...

  7. 服务器端升级为select模型处理多客户端

    流程图: select会定时的查询socket查询有没有新的网络连接,有没有新的数据需要读,有没有新的请求需要处理,一旦有新的数据需要处理,select就会返回,然后我们就可以处理相应的数据,sele ...

  8. Psychos in a Line CodeForces - 319B (单调栈的应用)

    Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...

  9. 在linux中创建新用户-再次安装python

    原来的阿里云python软件安装错了,用了root安装软件,搞得我后面的软件全部都要用root,软连接也搞不定,卸载也不好卸载.只能格式化,实例什么的都不用重建,系统也不用安装,直接创建用户就行了,磁 ...

  10. [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]

    [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...