力扣面试题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();
**/