当前位置:网站首页>Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
2022-07-03 10:06:00 【Cute at the age of three @d】
Hashtable + Balanced binary trees (TreeSet)
class LFUCache {
class Node{
int key;//key
int val;//val
int freq;// Frequency of use
int t;// The last time it was used
public Node(int key,int val,int freq,int t){
this.key = key;
this.val = val;
this.freq = freq;
this.t = t;
}
}
class NodeComparator implements Comparator<Node>{
// When the frequency of use is different Sort by usage frequency in ascending order
public int compare(Node n1,Node n2){
if(n1.freq != n2.freq)
return n1.freq - n2.freq;
else// When the frequency of use is the same Sort in ascending order of time used
return n1.t - n2.t;
}
}
private Map<Integer,Node> map;
private TreeSet<Node> set;
// Time counter from 0 Start
private int t;
//LFU The capacity of the cache
private int capacity;
public LFUCache(int capacity) {
this.map = new HashMap<>();
this.set = new TreeSet<>(new NodeComparator());
this.t = 0;
this.capacity = capacity;
}
public int get(int key) {
// Add one to the time counter
this.t += 1;
if(map.get(key) == null){
return -1;
}
else{
Node node = map.get(key);
set.remove(node);
// The key Frequency of use plus one
node.freq = node.freq + 1;
// to update key The last time used
node.t = this.t;
set.add(node);
map.put(key,node);
return node.val;
}
}
public void put(int key, int value){
if(this.capacity == 0)
return ;
this.t += 1;
// If key Already exists in map in
if(map.get(key)!=null){
Node node = map.get(key);
set.remove(node);
// to update val value
node.val = value;
// Update last used time
node.t = this.t;
// Use frequency plus one
node.freq = node.freq + 1;
set.add(node);
map.put(key,node);
}else{
// If LFU There is no space in the cache Pop up the most recently unused
if(set.size() >= this.capacity){
map.remove(set.first().key);
set.remove(set.first());
}
Node node = new Node(key,value,1,this.t);
map.put(key,node);
set.add(node);
}
}
}
/** * Your LFUCache object will be instantiated and called as such: * LFUCache obj = new LFUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */
Hashtable + Double linked list
class LFUCache {
class Node{
int key;//key
int val;//val
int freq;//key Frequency of use
Node prev;// Precursor pointer
Node next;// After drive pointer
public Node(int key,int val,int freq){
this.key = key;
this.val = val;
this.freq = freq;
}
}
class DoublyLinkedList{
// Double linked list
Node head;// The head pointer
Node tail;// Tail pointer
int size;// Number of nodes in the bidirectional linked list
public DoublyLinkedList(){
head = new Node(0,0,0);// Head node
tail = new Node(0,0,0);// Tail node
head.next = tail;
tail.prev = head;
size = 0;
}
public void addFirst(Node node){
Node headNext = head.next;
head.next = node;
node.prev = head;
node.next = headNext;
headNext.prev = node;
size += 1;
}
public void remove(Node node){
Node nodePrev = node.prev;
Node nodeNext = node.next;
nodePrev.next = nodeNext;
nodeNext.prev = nodePrev;
size -= 1;
}
public Node getHead(){
return head.next;
}
public Node getTail(){
return tail.prev;
}
}
// key, Node
private Map<Integer,Node> keyMap;
// Every frequency The corresponding nodes are placed in the bidirectional linked list The two-way linked list is sorted by operation time The longer the node has not been operated, the more backward it is in the linked list
private Map<Integer,DoublyLinkedList> freqMap;
//LFU Cache capacity
private int capacity;
// Minimum operating frequency
private int minFreq;
public LFUCache(int capacity) {
keyMap = new HashMap<>();
freqMap = new HashMap<>();
this.capacity = capacity;
minFreq = 0;
}
public int get(int key) {
if(this.capacity == 0)
return -1;
if(keyMap.get(key) == null){
return -1;
}
else{
Node node = keyMap.get(key);
DoublyLinkedList dl = freqMap.get(node.freq);
dl.remove(node);
if(dl.size == 0){
freqMap.remove(node.freq);
if(node.freq == minFreq)
minFreq += 1;
}
node.freq += 1;
if(freqMap.get(node.freq) == null)
freqMap.put(node.freq,new DoublyLinkedList());
freqMap.get(node.freq).addFirst(node);
keyMap.put(key,node);
return node.val;
}
}
public void put(int key, int value) {
if(this.capacity == 0)
return ;
if(keyMap.containsKey(key)){
Node node = keyMap.get(key);
DoublyLinkedList dl = freqMap.get(node.freq);
dl.remove(node);
if(dl.size == 0){
freqMap.remove(node.freq);
if(node.freq == minFreq)
minFreq += 1;
}
node.freq += 1;
node.val = value;
if(freqMap.get(node.freq) == null)
freqMap.put(node.freq,new DoublyLinkedList());
freqMap.get(node.freq).addFirst(node);
keyMap.put(key,node);
}
else{
if(keyMap.size() >= capacity){
Node node = freqMap.get(minFreq).getTail();
keyMap.remove(node.key);
freqMap.get(minFreq).remove(node);
if(freqMap.get(minFreq).size == 0)
freqMap.remove(minFreq);
}
Node newNode = new Node(key,value,1);
if(freqMap.get(newNode.freq) == null)
freqMap.put(newNode.freq,new DoublyLinkedList());
freqMap.get(newNode.freq).addFirst(newNode);
minFreq = 1;
keyMap.put(key,newNode);
}
}
}
/** * Your LFUCache object will be instantiated and called as such: * LFUCache obj = new LFUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */
边栏推荐
- Quelle langue choisir pour programmer un micro - ordinateur à puce unique
- STM32 general timer output PWM control steering gear
- (2) New methods in the interface
- el-table X轴方向(横向)滚动条默认滑到右边
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- El table X-axis direction (horizontal) scroll bar slides to the right by default
- Swing transformer details-2
- Stm32f04 clock configuration
- There is no specific definition of embedded system
- STM32 interrupt switch
猜你喜欢
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Gpiof6, 7, 8 configuration
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
YOLO_ V1 summary
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
03 FastJson 解决循环引用
随机推荐
Opencv notes 17 template matching
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Vgg16 migration learning source code
yocto 技术分享第四期:自定义增加软件包支持
51 MCU tmod and timer configuration
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
我想各位朋友都应该知道学习的基本规律就是:从易到难
is_ power_ of_ 2 judge whether it is a multiple of 2
My 4G smart charging pile gateway design and development related articles
LeetCode - 705 设计哈希集合(设计)
4G module board level control interface designed by charging pile
About windows and layout
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Not many people can finally bring their interests to college graduation
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
单片机学到什么程度能找到工作,这个标准不好量化
Drive and control program of Dianchuan charging board for charging pile design
Emballage automatique et déballage compris? Quel est le principe?