当前位置:网站首页>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(); */
边栏推荐
- Getting started with JMX, MBean, mxbean, mbeanserver
- 2021-01-03
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
- (1) 什么是Lambda表达式
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- 4G module at command communication package interface designed by charging pile
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
猜你喜欢
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
要选择那种语言为单片机编写程序呢
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
I think all friends should know that the basic law of learning is: from easy to difficult
Mysql database underlying foundation column
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
随机推荐
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
Adaptiveavgpool1d internal implementation
使用密钥对的形式连接阿里云服务器
要選擇那種語言為單片機編寫程序呢
QT detection card reader analog keyboard input
My 4G smart charging pile gateway design and development related articles
我想各位朋友都应该知道学习的基本规律就是:从易到难
4G module designed by charging pile obtains signal strength and quality
byte alignment
Liquid crystal display
2020-08-23
ADS simulation design of class AB RF power amplifier
Not many people can finally bring their interests to college graduation
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Problems encountered when MySQL saves CSV files
Pymssql controls SQL for Chinese queries
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
Yocto technology sharing phase IV: customize and add software package support
(1) What is a lambda expression
03 fastjason solves circular references