剑指offer 滑动窗口的最大值

方法

方法1:bisect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding:utf-8 -*-
class Solution:
def maxInWindows(self, num, size):
# write code here
import bisect
if size==0 or size>len(num):
return []
elif size==1:
return map(float,num)


self.window=num[:size]
self.window.sort()
res=[]
res.append(self.window[-1])

i=1
j=size
while j<len(num):
self.window.remove(num[i-1])
bisect.insort_left(self.window,num[j])
res.append(self.window[-1])
i+=1
j+=1
return res