当前位置:网站首页>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(); */
边栏推荐
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- 自动装箱与拆箱了解吗?原理是什么?
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- There is no specific definition of embedded system
- 2021-01-03
- 2020-08-23
- Windows下MySQL的安装和删除
- YOLO_ V1 summary
- Synchronization control between tasks
- ADS simulation design of class AB RF power amplifier
猜你喜欢

Mysql database underlying foundation column

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

MySQL root user needs sudo login

YOLO_ V1 summary

嵌入式系统没有特别明确的定义

CEF download, compile project

El table X-axis direction (horizontal) scroll bar slides to the right by default

新系列单片机还延续了STM32产品家族的低电压和节能两大优势

Getting started with JMX, MBean, mxbean, mbeanserver

Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
随机推荐
LeetCode - 673. 最长递增子序列的个数
01 business structure of imitation station B project
Exception handling of arm
2020-08-23
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Uniapp realizes global sharing of wechat applet and custom sharing button style
Do you understand automatic packing and unpacking? What is the principle?
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
El table X-axis direction (horizontal) scroll bar slides to the right by default
Crash工具基本使用及实战分享
LeetCode - 919. 完全二叉树插入器 (数组)
01仿B站项目业务架构
Oracle database SQL statement execution plan, statement tracking and optimization instance
A lottery like scissors, stone and cloth (C language)
自动装箱与拆箱了解吗?原理是什么?
Application of 51 single chip microcomputer timer
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
03 fastjason solves circular references