当前位置:网站首页>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(); */
边栏推荐
- MySQL root user needs sudo login
- 2.Elment Ui 日期选择器 格式化问题
- Basic knowledge of MySQL database (an introduction to systematization)
- Yocto technology sharing phase IV: customize and add software package support
- Basic knowledge of communication interface
- 4G module initialization of charge point design
- 2020-08-23
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- 03 fastjason solves circular references
- 没有多少人能够最终把自己的兴趣带到大学毕业上
猜你喜欢

Oracle database SQL statement execution plan, statement tracking and optimization instance

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

Not many people can finally bring their interests to college graduation

I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first

当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学

Timer and counter of 51 single chip microcomputer
![[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation](/img/06/7fd985faf8806ceface3864d4b3180.png)
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation

For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer

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

2. Elment UI date selector formatting problem
随机推荐
(1) 什么是Lambda表达式
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Notes on C language learning of migrant workers majoring in electronic information engineering
03 fastjason solves circular references
Swing transformer details-2
4G module designed by charging pile obtains signal strength and quality
Qcombox style settings
is_ power_ of_ 2 judge whether it is a multiple of 2
2020-08-23
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Wireshark use
嵌入式系统没有特别明确的定义
Simulate mouse click
自动装箱与拆箱了解吗?原理是什么?
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Development of intelligent charging pile (I): overview of the overall design of the system
Basic knowledge of communication interface
On the problem of reference assignment to reference