The problem involves finding the maximum value within a sliding window of size k
as it moves over an array of integers. This can be efficiently solved using a max-heap (priority queue) that stores pairs of indices and corresponding values. As the window slides, the heap helps in keeping track of the maximum value within the current window.
- Create a
Pair
struct that stores the index and value of an element. - Define a
PairHeap
type that implements heap functions (Len
,Less
,Swap
,Push
,Pop
) for managing the priority queue of pairs. The heap is ordered by values in descending order. If values are equal, it's further ordered by index in ascending order. - Initialize an empty priority queue (
pq
) of typePairHeap
. - Iterate through the array using two pointers
i
andj
, wherej
represents the right end of the sliding window. - Add elements to the priority queue while incrementing
j
. If the window size is less thank
, onlyj
is incremented. - When the window size equals
k
, start processing:- Remove elements from the heap that are outside the current window range (i.e., their indices are less than
i
). - Peek at the max element in the heap (the root of the max-heap).
- Store this max value in the
res
array at the corresponding index. - If the index of the max element is less than
i+1
, remove it from the heap. - Slide the window by incrementing
i
andj
.
- Remove elements from the heap that are outside the current window range (i.e., their indices are less than
- Continue this process until
j
reaches the end of the array. - Return the
res
array containing the maximum values within each sliding window.
- The max-heap operations (push and pop) take O(log k) time, where
k
is the window size. - Since each element is pushed and popped from the heap at most once, the overall time complexity is O(n log k), where
n
is the length of the input array.
- The space complexity is O(k), as the priority queue (
pq
) stores at mostk
elements.
import "container/heap"
type Pair struct {
index, value int
}
type PairHeap []Pair
func (h PairHeap) Len() int { return len(h) }
func (h PairHeap) Less(i, j int) bool { return h[i].value > h[j].value || (h[i].value == h[j].value && h[i].index < h[j].index) }
func (h PairHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *PairHeap) Push(x interface{}) {
*h = append(*h, x.(Pair))
}
func (h *PairHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func maxSlidingWindow(nums []int, k int) []int {
res := make([]int, len(nums)-k+1)
i, j, index := 0, 0, 0
pq := &PairHeap{}
heap.Init(pq)
for j < len(nums) {
pair := Pair{j, nums[j]}
heap.Push(pq, pair)
if j-i+1 < k {
j++
} else if j-i+1 == k {
for (*pq)[0].index < i {
heap.Pop(pq)
}
first := (*pq)[0]
res[index] = first.value
if first.index < i+1 {
heap.Pop(pq)
}
i++
j++
index++
}
}
return res
}