Problem Solving/LeetCode
2023.08.16 Today's Challenge
fabichoi
2023. 8. 16. 23:45
https://leetcode.com/problems/sliding-window-maximum/
Sliding Window Maximum - LeetCode
Can you solve this real interview question? 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 wind
leetcode.com
슬라이딩 윈도우 문제 풀기
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
dq = deque()
res = []
for i in range(k):
while dq and nums[i] >= nums[dq[-1]]:
dq.pop()
dq.append(i)
res.append(nums[dq[0]])
for i in range(k, len(nums)):
if dq and dq[0] == i - k:
dq.popleft()
while dq and nums[i] >= nums[dq[-1]]:
dq.pop()
dq.append(i)
res.append(nums[dq[0]])
return res
반응형