파비의 매일매일 공부기록

2023.08.16 Today's Challenge 본문

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
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.08.18 Today's Challenge  (0) 2023.08.18
2023.08.17 Today's Challenge  (0) 2023.08.17
2023.08.15 Today's Challenge  (0) 2023.08.15
2023.08.14 Today's Challenge  (0) 2023.08.14
2023.08.13 Today's Challenge  (0) 2023.08.13
Comments