-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_queue_.py
73 lines (48 loc) · 1.32 KB
/
_queue_.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from typing import List
from collections import deque
print('Queue')
class Queue:
def __init__(self):
self.front = []
self.back = []
def push(self, x: int) -> None:
self.back += [x]
def pop(self) -> int:
if not self.front:
self.front, self.back = self.back, []
self.front.reverse()
return self.front.pop()
def peek(self) -> int:
if not self.front:
self.front, self.back = self.back, []
self.front.reverse()
return self.front[-1]
def empty(self) -> bool:
if not self.back and not self.front:
return True
return False
def maxSlidingWindow(nums: List[int], k: int) -> List[int]:
'''
Given an array of integers and a window size k, return the maximum value in each window of size k.
'''
n = len(nums)
deq = deque()
ans = []
for i in range(n):
while deq and nums[i] > nums[deq[-1]]:
deq.pop()
if deq and i-k+1 > deq[0]:
deq.popleft()
deq.append(i)
if i >= k-1:
ans.append(nums[deq[0]])
return ans
nums = [1,3,-1,-3,5,3,6,7]
k = 3
print(maxSlidingWindow(nums, k)) # [3,3,5,5,6,7]
q = Queue()
q.push(1)
q.push(2)
print(q.peek()) # 1
print(q.pop()) # 1
print(q.empty()) # False