classMaxQueue { public: MaxQueue() { memset(q, 0, sizeof(q)); hh = 0; tt = -1; } intmax_value(){ if (dq.size()) return q[dq.front()]; elsereturn-1; } voidpush_back(int value){ q[++tt] = value; //if (dq.size() && value >= q[dq.front()]) dq.clear(); while (dq.size() && value > q[dq.back()]) dq.pop_back(); dq.push_back(tt); } intpop_front(){ if (tt < hh) return-1;
//当前的hh if (dq.size() && dq.front() == hh) { dq.pop_front(); }
return q[hh++]; }
private: int q[10010]; int hh; //队头 int tt; //队尾 std::deque<int> dq; };
/** * 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(); */