"Original" Leetcodeoj---Sliding window Maximum problem Solving report

Source: Internet
Author: User

God, I don't have the clout to shout "water" ...

Address of the topic:

https://leetcode.com/problems/sliding-window-maximum/

Topic content:

Given an array nums, there is a sliding window of size K which are moving from the very left of the array To the very right. You can only see the K numbers in the window. Each of the sliding window moves right by one position.

For example,
Given nums = [1,3,-1,-3,5,3,6,7] , and k = 3.

Window position                Max---------------               -----[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

Therefore, return the max sliding window as [3,3,5,5,6,7] .

Note:
You may assume K are always valid, Ie:1≤k≤input array's size for non-empty array.

Follow up:
Could solve it in linear time?

Topic Analysis:

The key is linear time. At the beginning I tried to solve the problem by the dynamic programming definition, however, there is no egg, suspect oneself has developed a certain degree of dynamic planning disease, need to reread clrs melted down

There is actually a second key point. Averaging complexity and strict complexity, there is no difference in the "linear time-resolved" requirement.

The third key point is that this is a dynamically maintained queue, and you want to maintain a maximum value without re-traversing the elements in the team.

I do not know if you have done O (1) time to maintain a stack of the most value of the problem, if not, you can look at this old article:

"Original" Leetcodeoj---Min Stack problem solving report

That said, just also say is essentially dynamic queue, now you take the stack out swindling, don't go after school

Don't worry, you can still use the stack to simulate the queue?

Two stacks, the push operation of the queue, the element is pressed to the stack 1. Queue of pop operations, first check whether the stack 2 is not empty, if the stack 2 has elements, direct pop. If the stack 2 has no elements, then the entire element of the current stack 1 is pressed into the stack 2.

In this way, we can maintain the maximum value in a stack and maintain the maximum value in a queue.

Because all the elements in the current queue are distributed across these two stacks, the maximum value of the two stacks is more than the last.

Someone asked, pop once, stack 2 there is something good, if not, you have to churn half a day, the stack 1 elements pressed into the stack 2, which is what linear time?

It's really linear time, don't forget to divide the complexity

Each element, into the team is pressed into the stack 1, the team was pressed into the stack 21 times, counted on the eject operation 2 times, altogether only 4 operations.

Altogether n elements

That's the 4n operation.

Isn't that linear?

The point is that an actual operation may only pop up once, and the operation of the stack is integrated at one of the popup sessions.

Specific code:

 Public classSolution { Public int[] Maxslidingwindow (int[] Nums,intk) {if(Nums.length = = 0) {            returnNums; }        int[] res =New int[Nums.length-k + 1]; Minqueue Queue=NewMinqueue ();  for(inti = 0; I < K; i + +) {Queue.push (nums[i]); } res[0] =Queue.getmax (); intindex = 1;  for(inti = k; i < nums.length; i + +) {queue.pop ();            Queue.push (Nums[i]); Res[index++] =Queue.getmax (); }        returnRes; }    classMinqueue {LinkedList<Integer> Stack1 =NewLinkedlist<integer>(); LinkedList<Integer> Stack2 =NewLinkedlist<integer>(); LinkedList<Integer> Maxone =NewLinkedlist<integer>(); LinkedList<Integer> Maxtwo =NewLinkedlist<integer>();  Public voidpush (Integer Item) {Pushone (item); }                 Publicinteger pop () {integer res=NULL; if(stack2.size () = = 0) {                 while(Stack1.size ()! = 0) {pushtwo (Popone ()); }} Res=Stack2.pop ();            Maxtwo.pop (); returnRes; }                 Publicinteger getmax () {integer one=Maxone.peek (); Integer=Maxtwo.peek (); if(One = =NULL) {                returnboth ; } Else if(both = =NULL){                returnOne ; }            returnOne > both?One:two; }                Privateinteger popone () {integer res=NULL;            Maxone.pop (); Res=Stack1.pop (); returnRes; }                Private voidPushone (Integer Item) {Stack1.push (item); if(stack1.size () = = 1) {Maxone.push (item); } Else{Integer Front=Maxone.peek (); if(Front <Item)                {Maxone.push (item); } Else{Maxone.push (front); }            }        }                Private voidpushtwo (Integer Item) {Stack2.push (item); if(stack2.size () = = 1) {Maxtwo.push (item); } Else{Integer Front=Maxtwo.peek (); if(Front <Item)                {Maxtwo.push (item); } Else{Maxtwo.push (front); }            }        }    }    }

"Original" Leetcodeoj---Sliding window Maximum problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.