【leetcode239】Sliding window maximum value -- go language implementation

Title description:

You are given an array of integers nums with a sliding window of size k moving from the far left of the array to the far right of the array. You can only see k numbers within the sliding window. The sliding window only moves to the right one bit at a time.
Returns the maximum value in a sliding window.

Example 1:

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation:
The position of the sliding window -- -------"Maximum value


[1 3 -1] -3 5 3 6 7 -------》 3
1 [3 -1 -3] 5 3 6 7 -------》 3
1 3 [-1 -3 5] 3 6 7 -------》 5
1 3 -1 [-3 5 3] 6 7 -------》 5
1 3 -1 -3 [5 3 6] 7 -------》 6
1 3 -1 -3 5 [3 6 7] -------》 7

Example 2:

Input: nums = [1], k = 1
Output: [1]

Idea analysis:

This is a classic problem with monotonic queues . At this point we need a queue. This queue puts the elements in the window, and then as the window moves, the queue also enters and exits. After each movement, the queue tells us what the maximum value inside is. Every time the window moves, call que.pop (the value of the element removed in the sliding window), que.push (the value of the element added in the sliding window), and then que.front() returns the maximum value we want.

Is such a queue delicious? If there is a ready-made data structure like this, wouldn't it be even more delicious!

Unfortunately, no! We need to implement such a queue ourselves.

Then analyze it, the elements in the queue must be sorted, and the maximum value must be placed at the exit of the queue, otherwise how do you know the maximum value.

But if you put all the elements in the window into the queue, when the window moves, the queue needs to pop up the elements.

So the question is, how can the sorted queue pop up the element to be removed from the window (this element may not necessarily be the maximum value).

Everyone should be thinking deeply at this time...

In fact, the queue does not need to maintain all the elements in the window , it only needs to maintain the elements that may become the maximum value in the window, and at the same time ensure that the values ​​​​of the elements in the queue are from large to small.

Then this queue that maintains monotonically decreasing elements is called a monotonically decreasing queue, that is, a monotonically decreasing or monotonically increasing queue. There is no direct support for monotonic queues in C++, we need to create a monotonic queue ourselves

Don't think that the monotonic queue implemented is to sort the numbers in the window. If it is sorted, what is the difference from the priority queue? When designing a monotonic queue, the pop and push operations should maintain the following rules:

pop(value):如果窗口移除的元素value等于单调队列的出口元素,那么队列弹出元素,否则不用任何操作
push(value):如果push的元素value大于入口元素的数值,那么就将队列入口的元素弹出,直到push元素的数值小于等于队列入口元素的数值为止

Keeping the above rules, every time the window moves, just ask que.front() to return the maximum value of the current window.

Code:

package stackAndQueue

type myQueue struct {
    
    
	queue []int
}
func NewMyQueue() *myQueue{
    
    
	return &myQueue{
    
    queue:[]int{
    
    }}
}
func (m *myQueue) Front()int{
    
    //弹出队首元素,也就是最大值
	return m.queue[0]
}
func (m *myQueue) Push(a int){
    
    
	for len(m.queue)!=0{
    
    
		if m.queue[len(m.queue)-1]<a{
    
    //因为要对队列的元素进行排序,所以把要比a小的元素全部移除,把a放置对应的位置
			m.queue=m.queue[0:len(m.queue)-1]
		}else{
    
    
			break
		}
	}
	m.queue=append(m.queue,a)
}
func (m *myQueue) Pop(a int){
    
    
	if m.Front()==a{
    
    //如果需要移除的元素正好是当前的最大值,那移除,否则什么都不做
		m.queue=m.queue[1:len(m.queue)]
	}
}
func maxSlidingWindow(nums []int, k int) []int {
    
    
	res:=[]int{
    
    }
	myQueue:=NewMyQueue()
	for i:=0;i<k;i++{
    
    
		myQueue.Push(nums[i])
	}
	res=append(res,myQueue.Front())
	myQueue.Pop(nums[0])
	for i:=k;i<len(nums);i++{
    
    
		myQueue.Push(nums[i])
		res=append(res,myQueue.Front())
		myQueue.Pop(nums[i-k+1])
	}
	return res
}

Guess you like

Origin blog.csdn.net/weixin_42918559/article/details/125159239