Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the
FreqStackclass:
FreqStack()constructs an empty frequency stack.void push(int val)pushes an integervalonto 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值,以及其对应的使用频率和到达时间代码
class FreqStack {Map valueToCount;PriorityQueue queue;int time;public FreqStack() {valueToCount = new HashMap<>();queue = new PriorityQueue<>(new Comparator() {@Overridepublic 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();*/
思路:在本题中,每次需要优先弹出频率最大的元素,如果频率最大元素有多个,则优先弹出靠近栈顶的元素。因此,我们可以考虑将栈序列分解为多个频率不同的栈序列,每个栈维护同一频率的元素。当元素入栈时频率增加,将元素加入到更高频率的栈中,低频率栈中的元素不需要出栈。当元素出栈时,将频率最高的栈的栈顶元素出栈即可。
实现
HashMap存储每个元素及其对应的使用频率maxFreq记录当前的最大使用频率HashMap> 存储每个频率对应的元素值 代码
class FreqStack {private Map freq;private Map> group;private int maxFreq;public FreqStack() {freq = new HashMap();group = new HashMap>();maxFreq = 0;}public void push(int val) {freq.put(val, freq.getOrDefault(val, 0) + 1);group.putIfAbsent(freq.get(val), new ArrayDeque());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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。