-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23.py
More file actions
21 lines (21 loc) · 759 Bytes
/
day23.py
File metadata and controls
21 lines (21 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from collections import deque
def sliding_window(arr,k):
#deque is used to store the indices of the array elements
result=[]
dq=deque()
for i in range(len(arr)):
if dq and dq[0]<i-k+1:
#it will remove the indices which has value greater than k
dq.popleft()
#in while statement it will remove the elements if they are smaller or = to current element
while dq and arr[dq[-1]]<=arr[i]:
dq.pop()
#add the current element index at the back of deque
dq.append(i)
#append the front element of the deque to the result when we have a complete window
if i>=k-1:
result.append(arr[dq[0]])
return result
arr = [1, 3, -1, -3, 5, 3, 6, 7]
k = 3
print(sliding_window(arr, k))