LeetCode stack and queue application - 239. Maximum sliding window

 What classic problems can stacks and queues solve? Every question can be found on my homepage, welcome everyone to pay attention~~

(1) Bracket matching problem (stack)

(2) String deduplication problem (stack)

  (3) Reverse Polish expression problem (stack)

  (4) The next larger element (monotonic stack)

(5) Receive rainwater (monotone stack)

(6) Sliding window maximum problem (monotonic queue) 

(7) Top K elements with the most occurrences (priority queue)

(8) Stack implementation queue

(9) Queue implementation stack 

1. Solution 

239. Maximum Sliding Window

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

Returns  the maximum value in a sliding window  .

 

class MyQueue {
        Stack<Integer> stack1;
        Stack<Integer> stack2;
    public MyQueue() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if(!stack2.isEmpty()){
            return stack2.pop();
        }
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
    
    public int peek() {
        if(!stack2.isEmpty()){
            return stack2.peek();
          }
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        return stack2.peek();
    }
    
    public boolean empty() {
        return stack1.isEmpty()&&stack2.isEmpty();
    }
}

2. Basic knowledge of the stack

The most notable feature of the stack is: first in last out

1. Common implementation classes of stacks in Java:

The most basic implementation: Stack<Integer> stack=new Stack<>();

Double-ended queue implementation: Deque<Integer> stack=new LinkedList<>();

2. Common methods of stack

push(x) -- push an element to the tail of the queue.
pop() -- removes an element from the head of the queue.
peek() -- returns the element at the head of the queue.
empty() -- returns whether the queue is empty.

3. Basic knowledge of queues

The most notable feature of the queue is: first in first out

1. Common implementation classes of queues in Java:

Ordinary queue: Queue<Integer> queue=new LinkedList<>();

Double-ended queue: Deque<Integer> queue=new LinkedList<>();

Priority queue: PriorityQueue<Integer> queue=new PriorityQueue<>();

2. Common methods of queues

add Add an element If the queue is full, throw a IIIegaISlabEepeplian exception
remove Remove and return the element at the head of the queue If the queue is empty, throw a NoSuchElementException
element Return the element at the head of the queue If the queue is empty, then Throws a NoSuchElementException

size The number of elements
offer Add an element and return true If the queue is full, return false
poll Remove and return the element at the head of the queue If the queue is empty, return null
peek Return the element at the head of the queue If the queue is empty, return null


put adds an element, blocks if the queue is full
take removes and returns the element at the head of the queue, blocks if the queue is empty

Guess you like

Origin blog.csdn.net/w20001118/article/details/127149079