ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

leetcode 895. Maximum Frequency Stack(最大频率栈)

2022-03-19 23:31:47  阅读:183  来源: 互联网

标签:int freqStack Maximum pop leetcode Frequency maxFreq push stack


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.

Example 1:

Input
[“FreqStack”, “push”, “push”, “push”, “push”, “push”, “push”, “pop”, “pop”, “pop”, “pop”]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
Output
[null, null, null, null, null, null, null, 5, 7, 5, 4]

Explanation
FreqStack freqStack = new FreqStack();
freqStack.push(5); // The stack is [5]
freqStack.push(7); // The stack is [5,7]
freqStack.push(5); // The stack is [5,7,5]
freqStack.push(7); // The stack is [5,7,5,7]
freqStack.push(4); // The stack is [5,7,5,7,4]
freqStack.push(5); // The stack is [5,7,5,7,4,5]
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].

写一个返回最大频率元素的stack。

思路:
既然是stack就要满足后进先出,还要记录频率。

那么需要一个变量maxFreq记录最大频率,
一个HashMap记录每个数字对应的出现频率。
当pop时,需要知道maxFreq对应的有哪些元素,及元素的push的先后顺序。
所以还需要一个HashMap用来保存每个出现频率对应的有哪些元素,这些元素要按push的顺序保存。

push时,首先要给对应数字的频率+1;该频率对应的list中加入push的数字。
然后更新maxFreq。
(注意:比如5出现了3次,那么map中(频率,数字list)对中要保存(1, [5]), (2, [5]), (3, [5]))

pop时,因为知道maxFreq,所以直接从HashMap中找到对应的数字list,
list中的数字是按push的顺序保存的,所以直接返回最后一个数字,并删除它;该数字对应的频率也要-1。
当list为空时,说明该maxFreq对应的已经没有数字了,maxFreq要-1。

class FreqStack {
    HashMap<Integer, List<Integer>> map = new HashMap<>();
    HashMap<Integer, Integer> freq = new HashMap<>();
    int maxFreq = 0;
    int fq = 0;
    
    public FreqStack() {
        
    }
    
    public void push(int val) {
        freq.put(val, freq.getOrDefault(val, 0) + 1);
        fq = freq.get(val);
        
        maxFreq = Math.max(maxFreq, fq);
        if(!map.containsKey(fq)) {
            map.put(fq, new ArrayList<Integer>());
        }
        map.get(fq).add(val);
    }
    
    public int pop() {
        int len = map.get(maxFreq).size();
        int element = map.get(maxFreq).remove(len - 1);
        freq.put(element, freq.get(element) - 1);
        if(len == 1) {
            maxFreq --;
        }
        return element;
    }
}

标签:int,freqStack,Maximum,pop,leetcode,Frequency,maxFreq,push,stack
来源: https://blog.csdn.net/level_code/article/details/123605250

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有