Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6a3c05a commit a984813Copy full SHA for a984813
data_structure/stack_queue.md
@@ -826,7 +826,26 @@ class Solution:
826
```
827
828
```Python
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
849
850
851
0 commit comments