LeetCode Interview Question 59-II. The maximum value of the queue [medium]

My solution:

1. I thought of using queue to traverse the queue when seeking the maximum value

class MaxQueue {
public:
    queue<int> q;
    
    MaxQueue() {

    }
    
    int max_value() {
        if(q.empty())   return -1;
        int a=0;
        for(int i=0;i<q.size();i++){
            if(q.front()>a) a=q.front();
            q.push(q.front());
            q.pop();
        }
        return a;
    }
    
    void push_back(int value) {
        q.push(value);
    }
    
    int pop_front() {
        if(q.empty())   return -1;
        int a=q.front();
        q.pop();
        return a;
    }
};

2. Most of the faster ones use deque. Thanks to this question, I also learn about deque

 

66 original articles published · Like1 · Visits 501

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105154860