Leetcode sword finger offer queue's largest java

Topic description
Define a queue and implement the function max_value to get the maximum value in the queue, requiring the function MAX_VALUE, PUSH_BACK, and POP_FRONT's average time complexity to be O (1).
If the team is empty, POP_FRONT and MAX_VALUE need to return -1
Method 1: Violence

class MaxQueue {
    int[] q = new int[20000];
    int begin = 0;
    int end = 0;

    public MaxQueue() {
        
    }
    
    public int max_value() {
        int ans = -1;
        for(int i = begin;i!=end;i++){
            if(q[i]>ans){
                ans = q[i];
            }
        }
        return ans;
    }
    
    public void push_back(int value) {
        q[end++] = value;
    }
    
    public int pop_front() {
        if(begin == end){
            return -1;
        }
        return q[begin++];

    }
}

Method Two:
Use oneDouble-end queue DequeWhen you enter the team, if DequeTailelementBe less thanThe element value of the team will be entered.Elements smaller than value all out of the teamAfterwards, enter the team; otherwise it will enter the team directly.

class MaxQueue {
    Queue<Integer> q;
    Deque<Integer> d; 

    public MaxQueue() {
        q = new LinkedList<Integer>();
        d = new LinkedList<Integer>();
    }
    
    public int max_value() {
        if(d.isEmpty()){
            return -1;
        }
        return d.peekFirst();// Get the first element of the double-end queue
    }
    
    public void push_back(int value) {
        while(!d.isEmpty()&& d.peekLast()<value){
            d.pollLast();// Get and remove the last element of the double-end queue
        }
        d.offerLast(value);/ / Insert the specified element into the end of the double-end queue
        q.offer(value);/ / Add the specified element to the end of this queue
    }
   
    public int pop_front() {
        if(q.isEmpty()){
            return -1;
        }
        int ans = q.poll();/ / Get the element of the team head and delete the element
        if(ans == d.peekFirst()){
            d.pollFirst();// Get and remove the first element of the double-end queue
        }
        return ans;
    }
}

/**
 * Your MaxQueue object will be instantiated and called as such:
 * MaxQueue obj = new MaxQueue();
 * int param_1 = obj.max_value();
 * obj.push_back(value);
 * int param_3 = obj.pop_front();
 */

Intelligent Recommendation

Leetcode plus sword finger offer

Remember three things: 1. Code standardization, indentation, variable naming, 2. Code integrity, considering all boundary inputs 3. Code robustness, consider error handling, defensive programming The ...

LeetCode Sword Finger Offer Series

Article Directory 03. Repeated numbers in the array simple analysis code 04. Search in a two-dimensional array analysis code 07. Rebuild Binary Tree analysis code 39. Numbers that occur more than half...

LeetCode-Sword Finger Offer Interview Question 32 (JAVA)

The tree is traversed first, and then the array traversed in the middle is reversed. Title: Interview Question 32-III. Print the binary tree from top to bottom III Please implement a function to print...

LeetCode "Sword Finger Offer", with Java problem-solving ideas (19)

Hi everybody, I'mFangyuan Everyone wants to cheer up today Question number Sword Point Offer 17. Print from 1 to the largest n digits Sword Point Offer 18. Delete the node of the linked list Sword Poi...

Leetcode sword finger offer symmetrical binary tree Java

Topic description Implement a function to determine a binary tree is not symmetrical. If a binary tree is like a mirror, then it is symmetrical. Recursive method: Termination condition: When L and R a...

More Recommendation

Leetcode sword finger offer 57. and two numbers for s Java

And two numbers for s Do question blog link Title link describe Example Initial Code Template Code Do question blog link Title link https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/ de...

Leetcode sword finger offer 13. Motion range Java

Motion range of robots Do question blog link Title link describe Example Initial Code Template Code Do question blog link Title link https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof...

[Golang] LeetCode-Sword Finger Offer-Interview Question 54-The kth largest node of the binary search tree [Solution and Optimization]

topic Source: LeetCode Link: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof Problem-solving ideas Binary Search Tree, (Again: binary search tree, binary sort tree) It is e...

LeetCode (Sword Finger Offer-tree)-Interview Question 54. The k-th largest node of the binary search tree

Given a binary search tree, please find the k-th largest node in it. Example 1: Example 2: limit: Question source link: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof Prob...

Sword Finger Offer LeetCode Interview Questions 17. Print from 1 to the largest n digits

Interview Question 17. Print from 1 to the largest n digits Enter the number n, and print out in order from 1 to the largest n-digit decimal number. For example, if you enter 3, 1, 2, and 3 will be pr...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top