Hard
Array
Queue
Sliding Window
Heap
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 window. Each time the sliding window moves right by one position.
Return the max sliding window.
Example 1:
Example 2:
Constraints:
nums.length
<= 105nums[i]
<= 104k
<= nums.length
思路:
nums[i]
做以下處理:
nums[i]
的元素索引。這保證了 deque 中的索引值對應的元素值是降序排列。nums[i]
的索引值放入 deque 的尾部。SheepWed, 16 Aug 2023
原本用 heap 寫,但 heap 移除指定資料沒有比較快,看說明用 deque 就好
C# 沒有 deque, 也可以用 LinkedList 替代,這邊寫一個堪用、不安全的 deque
JimAug 16, 2023