Top 150 Interviewハード

Sliding Window Maximum

Detailed guide and Python implementation for the 'Sliding Window Maximum' problem.

問題提起

ハード

You are given an array of integers nums and an integer k representing the size of a sliding window which moves from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max value in each sliding window.

Write a function maxSlidingWindow(nums: List[int], k: int) -> List[int].

制約
  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= len(nums)

Example 1
Input
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
Output
[3, 3, 5, 5, 6, 7]
Explanation

Window [1,3,-1] max=3, [3,-1,-3] max=3, [-1,-3,5] max=5, [-3,5,3] max=5, [5,3,6] max=6, [3,6,7] max=7.

Example 2
Input
nums = [1], k = 1
Output
[1]
Explanation

Single element window, max is 1.

Need a Hint?
Consider using Sliding Window-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。