Google interview Question:count bounded slices (min/max queue)

Source: Internet
Author: User

Question:

A Slice of an array said to be a bounded Slice if Max (Slicearray)-min (Slicearray) <=k.

If Array [3,5,6,7,3] and k=2 provided: The number of bounded slice is 9,

First Slice (0,0) in the array Min (0,0) =3 Max (0,0) =3 max-min<=k result 0<=2 so it's bounded slice

Second slice (0,1) in the array Min (0,1) =3 Max (0,1) =5 max-min<=k result 2<=2 so it's bounded slice

Second Slice (0,2) in the array Min (0,1) =3 Max (0,2) =6 max-min<=k result 3<=2 so it's not bounded slice

In this "can find" that there is nine bounded slice.

(0, 0), (0, 1), (1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3), (4, 4).

The direct approach is to scan each element of the array to the right, record the Min/max, select the qualifying slice as the result, and the time complexity to O (n^2).

We find that the element of index I in the array does not satisfy the condition when it is scanned to the right, so the slice after the starting J of I is impossible to satisfy the condition, so I need to move right I, which is equivalent to maintaining a sliding window satisfying max-min<=k. Then the algorithm can be designed as follows: (1) Create a Min/max queue that requires push,pop,peek,getmax,getmin to be a constant time. (2) Iterate through the array, push the element if the current element does not cause the element in the queue to satisfy the condition, and if the current element causes the element in the queue to not satisfy the condition, output all results that satisfy the condition (for example, the queue header index is 2 and the current index is 5, then (2,2), (2,3) Added to the result), the pop queue header element, again checks whether the current element can enter the queue. The time complexity of this algorithm is O (N).

So how do we achieve the Min/max queue we're asking for? We know that only the queue header and the tail of the queue can be manipulated (the Java queue interface is LinkedList implemented, so it is possible to traverse the elements inside the queue with iterator, but if the Traverse queue is the maximum minimum value, the time complexity is O (N)). Before implementing the Min/max queue, let's look at how to implement the Min/max stack.

https://leetcode.com/problems/min-stack/

Leetcode's question shows how to build a Min/max stack. We need to build an extra stack to record the maximum value, and an extra stack to record the minimum value.

classminmaxstack{Privatedeque<integer> stack =NewLinkedlist<>(); PrivateDeque<integer> Minstack =NewLinkedlist<>(); PrivateDeque<integer> maxstack =NewLinkedlist<>();  Public voidPushintx) {stack.push (x); if(Minstack.isempty () | | Minstack.peek () >=x) Minstack.push (x); if(Maxstack.isempty () | | Maxstack.peek () <=x) Maxstack.push (x); }     Public intpop () {intTMP =Stack.pop (); if(!minstack.isempty () && minstack.peek () = =tmp) Minstack.pop (); if(!maxstack.isempty () && maxstack.peek () = =tmp) Maxstack.pop (); returntmp; }     Public intPeek () {returnStack.peek (); }     Public BooleanIsEmpty () {returnStack.isempty (); }     Public intgetmin () {if(!minstack.isempty ())returnMinstack.peek (); Else returnInteger.max_value; }         Public intGetmax () {if(!maxstack.isempty ())returnMaxstack.peek (); Else returnInteger.min_value; }}

With Min/max Stack, we can build Min/max queue. How do I use a stack to simulate a queue? We use two stacks to implement the queue operation. When the queue performs an offer operation, we push this element to stack 1, and when the queue performs the poll operation, if Stack 2 is empty, we pop-push all the elements in stack 1 to stack 2 and then pop from Stack 2. You can see that the offer/poll operation of the simulated queue has an O (1) time-out complexity. We know Queuemax=max (Stack1max, Stack2max), Queuemin=min (Stack1min, stack2min).

classminmaxqueue{minmaxstack stack1=NewMinmaxstack (); Minmaxstack Stack2=NewMinmaxstack ();  Public voidOfferintx)    {Stack1.push (x); }     Public intpoll () {if(!Stack2.isempty ()) {            returnStack2.pop (); }         while(!Stack1.isempty ())        {Stack2.push (Stack1.pop ()); }        returnStack2.pop (); }     Public BooleanIsEmpty () {returnStack1.isempty () &&Stack2.isempty (); }     Public intgetmin () {returnmath.min (Stack1.getmin (), stack2.getmin ()); }     Public intGetmax () {returnMath.max (Stack1.getmax (), Stack2.getmax ()); }}

Google interview Question:count bounded slices (min/max queue)

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.