当前位置:网站首页>Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
2022-07-03 10:05:00 【Cute at the age of three @d】


Hashtable + Priority queue

class FreqStack {
class Node{
// Value size
int val;
// Frequency of occurrence of value
int freq;
// The time when the value appears The greater the time, the closer to the top of the stack
int t;
public Node(int val,int freq,int t){
this.val = val;
this.freq = freq;
this.t = t;
}
}
class NodeComparator implements Comparator<Node> {
// If the occurrence frequency is different, sort in reverse order by frequency
public int compare(Node n1, Node n2) {
if(n2.freq != n1.freq)
return n2.freq - n1.freq;
else// If the frequency of occurrence is the same, sort in reverse order according to the entry time
return n2.t - n1.t;
}
}
private Map<Integer,Integer> map;// Record the frequency of each value
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(); */
Double hash table + Stack

class FreqStack {
// Record the frequency of each value
private Map<Integer,Integer> freqMap;
// Record each frequency Corresponding value Value is put in the stack
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 Add one to the corresponding frequency
freqMap.put(val,freqMap.getOrDefault(val,0) + 1);
int f = freqMap.get(val);
// If val The corresponding frequency has exceeded the maximum frequency
if(f > maxFreq)
maxFreq = f;// Update maximum frequency
if(freqStack.get(f) == null)// Push
freqStack.put(f,new LinkedList<>());
freqStack.get(f).offerFirst(val);
}
public int pop(){
// From the stack corresponding to the maximum frequency Pop up the top of the stack
int val = freqStack.get(maxFreq).pollFirst();
// The frequency corresponding to this value is minus one
freqMap.put(val,freqMap.getOrDefault(val,0) - 1);
// If the stack corresponding to the maximum frequency is empty
if(freqStack.get(maxFreq).size() == 0)
{
//freqStack Remove the maximum frequency
freqStack.remove(maxFreq);
// Reduce the maximum frequency by one
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(); */
边栏推荐
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- 4G module board level control interface designed by charging pile
- Toolbutton property settings
- Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
- Design of charging pile mqtt transplantation based on 4G EC20 module
- SCM is now overwhelming, a wide variety, so that developers are overwhelmed
- An executable binary file contains more than machine instructions
- On the problem of reference assignment to reference
- 对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
猜你喜欢

手机都算是单片机的一种,只不过它用的硬件不是51的芯片

Uniapp realizes global sharing of wechat applet and custom sharing button style

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

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

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

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

应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机

QT is a method of batch modifying the style of a certain type of control after naming the control

嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走

要選擇那種語言為單片機編寫程序呢
随机推荐
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Do you understand automatic packing and unpacking? What is the principle?
Basic knowledge of communication interface
getopt_ Typical use of long function
Wireshark use
(2)接口中新增的方法
pycharm 无法引入自定义包
My 4G smart charging pile gateway design and development related articles
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
51 MCU tmod and timer configuration
The underlying principle of vector
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Vgg16 migration learning source code
LeetCode - 933 最近的请求次数
Timer and counter of 51 single chip microcomputer
STM32 general timer output PWM control steering gear