LeetCode 895. Maximum Frequency Stack

题目地址:https://leetcode.com/problems/maximum-frequency-stack/description/

优先队列模拟栈

class FreqStack {
public:
    struct Node
    {
        int x,y,p;
        bool operator < (const Node &N) const
        {
            if(N.x==x)
                return y pq;
    map mp;
    int cnt;
    FreqStack() {
        cnt=0;
    }
    
    void push(int x) {
        Node node;
        node.p=x;
        node.x=++mp[x];
        node.y=cnt++;
        pq.push(node);
    }
    int pop() {
        Node node=pq.top();
        pq.pop();
        mp[node.p]--;
        return node.p;
    }
};

/**
 * Your FreqStack object will be instantiated and called as such:
 * FreqStack obj = new FreqStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 */

 

你可能感兴趣的:(LeetCode周赛,数据结构)