Skip to content

Commit a984813

Browse files
authored
Update stack_queue.md
1 parent 6a3c05a commit a984813

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

data_structure/stack_queue.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,26 @@ class Solution:
826826
```
827827

828828
```Python
829-
829+
class Solution:
830+
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
831+
if k == 1:
832+
return nums
833+
result = []
834+
# max index deque
835+
max_index = collections.deque()
836+
length = len(nums)
837+
for i in range(length):
838+
# max left index < current left index
839+
if max_index and max_index[0] == i - k:
840+
max_index.popleft()
841+
# update new max num index
842+
while max_index and nums[max_index[-1]] < nums[i]:
843+
max_index.pop()
844+
max_index.append(i)
845+
# after k-1 index, record max number
846+
if i >= k - 1:
847+
result.append(nums[max_index[0]])
848+
return result
830849
```
831850

832851

0 commit comments

Comments
 (0)