当前位置:网站首页>LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
2022-07-03 09:20:00 【三岁就很萌@D】


哈希表+优先队列

class FreqStack {
class Node{
//值的大小
int val;
//值出现的频率
int freq;
//值出现的时间 时间越大越靠近栈顶
int t;
public Node(int val,int freq,int t){
this.val = val;
this.freq = freq;
this.t = t;
}
}
class NodeComparator implements Comparator<Node> {
//如果出现频率不一样按频率逆序排序
public int compare(Node n1, Node n2) {
if(n2.freq != n1.freq)
return n2.freq - n1.freq;
else//如果出现频率一样按进入时间逆序排序
return n2.t - n1.t;
}
}
private Map<Integer,Integer> map;//记录每个值出现的频率
private PriorityQueue<Node> queue;
private int t;
public FreqStack() {
map = new HashMap<>();
queue = new PriorityQueue<>(new NodeComparator());
t = 0;
}
public void push(int val) {
map.put(val,map.getOrDefault(val,0) + 1);
queue.offer(new Node(val,map.getOrDefault(val,0),t++));
}
public int pop() {
int val = queue.poll().val;
map.put(val,map.getOrDefault(val,0) - 1);
if(map.getOrDefault(val,0) == 0)
map.remove(val);
return val;
}
}
/** * Your FreqStack object will be instantiated and called as such: * FreqStack obj = new FreqStack(); * obj.push(val); * int param_2 = obj.pop(); */
双哈希表 + 栈

class FreqStack {
//记录每个值出现的频率
private Map<Integer,Integer> freqMap;
//记录每个频率 对应的值 值放在栈里面
private Map<Integer,Deque<Integer>> freqStack;
private int maxFreq;
public FreqStack() {
freqMap = new HashMap<>();
freqStack = new HashMap<>();
maxFreq = 0;
}
public void push(int val) {
//val 对应频率加一
freqMap.put(val,freqMap.getOrDefault(val,0) + 1);
int f = freqMap.get(val);
//如果val对应的频率已经超过最大频率
if(f > maxFreq)
maxFreq = f;//更新最大频率
if(freqStack.get(f) == null)//入栈
freqStack.put(f,new LinkedList<>());
freqStack.get(f).offerFirst(val);
}
public int pop(){
//从最大频率对应的栈 弹出栈顶
int val = freqStack.get(maxFreq).pollFirst();
//该值对应的频率减一
freqMap.put(val,freqMap.getOrDefault(val,0) - 1);
//如果最大频率对应的栈空了
if(freqStack.get(maxFreq).size() == 0)
{
//freqStack移除最大频率
freqStack.remove(maxFreq);
//将最大频率减一
maxFreq -= 1;
}
return val;
}
}
/** * Your FreqStack object will be instantiated and called as such: * FreqStack obj = new FreqStack(); * obj.push(val); * int param_2 = obj.pop(); */
边栏推荐
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- STM32 interrupt switch
- Toolbutton property settings
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
- 2020-08-23
- Crash工具基本使用及实战分享
- Adaptiveavgpool1d internal implementation
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
- getopt_ Typical use of long function
猜你喜欢

RESNET code details

Serial communication based on 51 single chip microcomputer

2021-10-27

LeetCode - 673. 最长递增子序列的个数

51 MCU tmod and timer configuration

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

LeetCode - 919. 完全二叉树插入器 (数组)

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

SCM is now overwhelming, a wide variety, so that developers are overwhelmed

一个可执行的二进制文件包含的不仅仅是机器指令
随机推荐
YOLO_ V1 summary
MySQL的简单使用(增删改查)
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Google browser plug-in recommendation
Swing transformer details-1
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Leetcode 300 最长上升子序列
2020-08-23
Pymssql controls SQL for Chinese queries
Basic knowledge of communication interface
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
yocto 技术分享第四期:自定义增加软件包支持
Project scope management__ Scope management plan and scope specification
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
没有多少人能够最终把自己的兴趣带到大学毕业上
JS基础-原型原型链和宏任务/微任务/事件机制
Getting started with JMX, MBean, mxbean, mbeanserver
Idea remote breakpoint debugging jar package project
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
Toolbutton property settings