其他分享
首页 > 其他分享> > 力扣面试题59 - II. 队列的最大值

力扣面试题59 - II. 队列的最大值

作者:互联网

力扣面试题59 - II. 队列的最大值

class MaxQueue {
    queue<int> m_Que;
    deque<int> m_Deq;
public:
    MaxQueue()
    {

    }
    
    int max_value() 
    {
        if (m_Deq.empty())
        {
            return -1;
        }
        return m_Deq.front();
    }
    
    void push_back(int value) 
    {
        while (!m_Deq.empty() && m_Deq.back() < value)
            m_Deq.pop_back();
        m_Deq.push_back(value);
        m_Que.push(value);
    }
    
    int pop_front()
    {
        if (m_Que.empty())
        {
            return -1;
        }
        int value = m_Que.front();
        m_Que.pop();
        if (m_Deq.front() == value)
        {
            m_Deq.pop_front();
        }
        return value;
    }

};

/**

 * 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();
**/

标签:面试题,59,int,Deq,back,value,II,pop,front
来源: https://www.cnblogs.com/woodjay/p/12437044.html