Skip to content

Commit b47e252

Browse files
committed
explanation of sliding-window-maximum.py
1 parent 7339f2f commit b47e252

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Sliding-Window/sliding_window_maximum.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import collections
22

3+
# O(n)
4+
# initialize list to represent list of maximum number in sliding window
5+
# we initialize a queue for setting the max in sliding window at partcular iteration
6+
# initialize the left and right pointer at 0 since we are using while
7+
# while right pointer is less than length of list (setting the boundary)
8+
# then we need to add the current index to queue
9+
# if there are value in queue and queue value is less than current, we pop the queue
10+
# then insert current value
11+
# if our window is out of bounds remove the leftmost value (end of cur iter)
12+
# if our loop is equal to sliding wiwndow size 'k' then append the max value(leftmost) and increase
13+
# left pointer by 1
14+
# we need to increase r by 1 in every iteration
15+
316

417
class Solution:
518
def maxSlidingWindow(self, nums: list[int], k: int) -> list[int]:

0 commit comments

Comments
 (0)