leetcode-hard-array-239. Sliding Window Maximum
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的更多相关文章
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- [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 ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- [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 ...
- 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 ...
- 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 ...
- (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 ...
- leetcode 239 Sliding Window Maximum
这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...
- [leetcode] #239 Sliding Window Maximum (Hard)
原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...
随机推荐
- JPanel实现滚动条
之前一直用JScrollPane里面放一个JTextArea,就可以在文本框内实现滚动条. 但是最近做一个小demo,需要在JPanel中实现滚动条,就找了下资料,做好了,现在记录一下,防止以后再用到 ...
- vue2.0+按需引入element-ui报错
项目使用vue脚手架自动生成的,vue版本为^2.5.16.项目中需要按需使用element-ui,根据element-ui的官方文档,一开始在babel.config.js文件中修改配置 modul ...
- 2.Struts2-Action
struts.xml文件中 action 标签中几个属性的作用 1.name:为action命名,输入url访问时,需要带上action的name,通过name知道访问哪个action(通过class ...
- ASE19团队项目alpha阶段model组 scrum1 记录
本次会议于11月1日,19时整在微软北京西二号楼13478召开,持续15分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing Wu, Yu ...
- Mybatis和hibernate的优缺点比较
介绍: Hibernate :Hibernate 是当前最流行的ORM框架,对数据库结构提供了较为完整的封装. Mybatis:Mybatis同样也是非常流行的ORM框架,主要着力点在于POJO 与S ...
- Oracle笔记(二) SQLPlus命令
对于Oracle数据库操作主要使用的是命令行方式,而所有的命令都使用sqlplus完成,对于sqlplus有两种形式. 一种是dos风格的sqlplus:sqlplus.exe; 另一种是window ...
- 【转】sscanf函数用法实例
sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, string fmt, mixed var1, mixed var2 . ...
- POM标签大全详解
父(Super) POM <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "htt ...
- java——比较难和底层的面试题
链接地址:https://mp.weixin.qq.com/s/lnbCysCQgfjF_kcB83KQZg 这是一个在线教育机构的文章,感觉大部分都不会,太难了. 一.自我介绍 二.多线程相关: 线 ...
- 延长zencart1.5.x后台的15分钟登录时间和取消90天强制更换密码
延长zencart1.5.x后台的15分钟登录时间 打开includes\functions\sessions.php if (IS_ADMIN_FLAG === true) { if (!$SESS ...