【每日一题Day42】最大频率栈 | 哈希表+大顶堆 哈希表+栈

最大频率栈【LC895】

Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.

Implement the FreqStack class:

  • FreqStack() constructs an empty frequency stack.
  • void push(int val) pushes an integer val onto the top of the stack.
  • int pop()removes and returns the most frequent element in the stack.
    • If there is a tie for the most frequent element, the element closest to the stack’s top is removed and returned.

解封啦 但是真的好冷 不愿出门

哈希表+大顶堆
  • 思路:

    按照题意,每次弹出使用频率最高的元素,如果使用频率相等,那么弹出最近放入栈中的元素。题目中的使用频率指放入最大频率栈的次数,不包括弹出的次数。

    因此,我们可以使用HashMap记录每个元素的使用频率;并构造节点Node,将新节点的元素值、使用频率和到达时间记录其中;使用大顶堆存储节点,每次弹出顶部节点即可,弹出后需要更新使用频率。

  • 实现:

    • 使用valueToCount存储每个元素及其对应的使用频率即次数
    • 构造节点node存储val值,以及其对应的使用频率和到达时间
    • 使用优先队列存放节点,优先队列以节点的使用频率从大到小、到达时间从大到小的顺序排序【大顶堆】:
      • 每次添加一个元素都构造一个新节点,存放该数值的使用频率以及到达时间
      • 每次删除一个元素都将优先队列使用频率最高到达时间最近的节点删除,并将map集合中的次数-1
  • 代码

    class FreqStack {
        Map<Integer,Integer> valueToCount;
        PriorityQueue<Node> queue;
        int time;
        public FreqStack() {
            valueToCount = new HashMap<>();
            queue = new PriorityQueue<>(new Comparator<Node>() {
                @Override
                public int compare(Node node1, Node node2) {
                    if (node2.count == node1.count){
                        return node2.time - node1.time;
                    }
                    return node2.count - node1.count;
                }
            });
        }
        
        public void push(int val) {
            int count = valueToCount.getOrDefault(val,0) + 1;
            valueToCount.put(val, count);
            Node node = new Node(val,count,++time);
            queue.add(node);               
        }
        
        public int pop() {
            Node delNode = queue.poll();
            int val = delNode.val;
            valueToCount.put(val,valueToCount.get(val) - 1);
            return val;
        }
    
    }
    class Node{
        int val;
        int count;
        int time;
        public Node(int val, int count, int time){
            this.val = val;
            this.count = count;
            this.time = time;
        }
        // public int compareTo(Object o) {
        //     ListNode o1 = (ListNode)o;
        //     return o1.count - this.count;
        // }
    }
    /**
     * Your FreqStack object will be instantiated and called as such:
     * FreqStack obj = new FreqStack();
     * obj.push(val);
     * int param_2 = obj.pop();
     */
    
    • 复杂度
      • 时间复杂度: O ( l o g n ) O(logn) O(logn),构建优先队列需要 O ( l o g n ) O(logn) O(logn)的时间复杂度,push和pop方法均需要 O ( l o g n ) O(logn) O(logn)的时间复杂度
      • 空间复杂度: O ( n ) O(n) O(n)
哈希表+多个栈
  • 思路:在本题中,每次需要优先弹出频率最大的元素,如果频率最大元素有多个,则优先弹出靠近栈顶的元素。因此,我们可以考虑将栈序列分解为多个频率不同的栈序列,每个栈维护同一频率的元素。当元素入栈时频率增加,将元素加入到更高频率的栈中,低频率栈中的元素不需要出栈。当元素出栈时,将频率最高的栈的栈顶元素出栈即可。

  • 实现

    • 使用HashMap<Integer,Integer>存储每个元素及其对应的使用频率
    • 使用maxFreq记录当前的最大使用频率
    • 使用HashMap<Integer,Deque<Integer>>存储每个频率对应的元素值
      • push:将该元素压入对应的频率栈中
      • pop:将最高频率的栈的栈顶元素出栈
  • 代码

    class FreqStack {
        private Map<Integer, Integer> freq;
        private Map<Integer, Deque<Integer>> group;
        private int maxFreq;
    
        public FreqStack() {
            freq = new HashMap<Integer, Integer>();
            group = new HashMap<Integer, Deque<Integer>>();
            maxFreq = 0;
        }
    
        public void push(int val) {
            freq.put(val, freq.getOrDefault(val, 0) + 1);
            group.putIfAbsent(freq.get(val), new ArrayDeque<Integer>());
            group.get(freq.get(val)).push(val);
            maxFreq = Math.max(maxFreq, freq.get(val));
        }
    
        public int pop() {
            int val = group.get(maxFreq).peek();
            freq.put(val, freq.get(val) - 1);
            group.get(maxFreq).pop();
            if (group.get(maxFreq).isEmpty()) {
                maxFreq--;
            }
            return val;
        }
    }
    
    作者:力扣官方题解
    链接:https://leetcode.cn/problems/maximum-frequency-stack/solutions/1997251/zui-da-pin-lu-zhan-by-leetcode-solution-moay/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    • 复杂度
      • 时间复杂度: O ( 1 ) O(1) O(1),构建优先队列需要 O ( l o g n ) O(logn) O(logn)的时间复杂度,push和pop方法均需要 O ( 1 ) O(1) O(1)的时间复杂度
      • 空间复杂度: O ( n ) O(n) O(n)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值