-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(239) Sliding Window Maximum.py
97 lines (74 loc) · 2.35 KB
/
(239) Sliding Window Maximum.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Stack:
def __init__(self):
self.stack = []
def push(self, elem):
self.stack.append(elem)
def pop(self):
return self.stack.pop()
def __len__(self):
return len(self.stack)
class MaxStack(Stack):
def max(self):
if len(self.stack) == 0:
return None
return self.stack[-1][1]
def push(self, elem):
maxElem = self.max()
if maxElem is None or elem >= maxElem:
super().push((elem, elem))
else:
super().push((elem, maxElem))
def pop(self):
elem, _ = super().pop()
return elem
class MaxQueue:
def __init__(self):
self.eq = MaxStack()
self.dq = MaxStack()
def enqueue(self, elem):
self.eq.push(elem)
def dequeue(self):
if len(self.dq) == 0:
while len(self.eq) > 0:
self.dq.push(self.eq.pop())
if len(self.dq) == 0:
raise Exception("Nothing in the queue to dequeue.")
return self.dq.pop()
def max(self):
Leq = len(self.eq)
Ldq = len(self.dq)
if Leq == 0 and Ldq == 0:
return None
elif Leq != 0 and Ldq == 0:
return self.eq.max()
elif Leq == 0 and Ldq != 0:
return self.dq.max()
else:
return max(self.eq.max(), self.dq.max())
def __len__(self):
return len(self.eq) + len(self.dq)
"""
(239) Sliding Window Maximum
https://leetcode.com/problems/sliding-window-maximum/
You are given an array of integers nums, there is a sliding window of size k
which is moving from the very left of the array to the very right.
You can only see the k numbers in the window. Each time the sliding window moves right by one position.
Return the max sliding window.
"""
class Solution:
def maxSlidingWindow(self, nums, k):
mq = MaxQueue()
for i in range(k):
mq.enqueue(nums[i])
maxWindow = [mq.max()]
for i in range(k, len(nums)):
mq.enqueue(nums[i])
mq.dequeue()
maxWindow.append(mq.max())
return maxWindow
if __name__ == '__main__':
sol = Solution()
nums = [1,3,-1,-3,5,3,6,7]
k = 3
print("INPUT:", nums)
print("OUTPUT:", sol.maxSlidingWindow(nums, k))