当前位置:网站首页>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(); */
边栏推荐
- RESNET code details
- Tensorflow built-in evaluation
- Which language should I choose to program for single chip microcomputer
- [keil5 debugging] warning:enumerated type mixed with other type
- After clicking the Save button, you can only click it once
- Retinaface: single stage dense face localization in the wild
- pycharm 无法引入自定义包
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- 2021-11-11 standard thread library
猜你喜欢

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

Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from

The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving

干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
![[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

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

Dictionary tree prefix tree trie

There is no specific definition of embedded system

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

It is difficult to quantify the extent to which a single-chip computer can find a job
随机推荐
01仿B站项目业务架构
Opencv notes 20 PCA
(2)接口中新增的方法
When the reference is assigned to auto
Application of external interrupts
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Vgg16 migration learning source code
Yocto technology sharing phase IV: customize and add software package support
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
4G module at command communication package interface designed by charging pile
QT is a method of batch modifying the style of a certain type of control after naming the control
Adaptiveavgpool1d internal implementation
STM32 general timer output PWM control steering gear
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
LeetCode - 933 最近的请求次数
In third tier cities and counties, it is difficult to get 10K after graduation
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
QT setting suspension button