当前位置:网站首页>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); */
边栏推荐
- QT is a method of batch modifying the style of a certain type of control after naming the control
- Drive and control program of Dianchuan charging board for charging pile design
- 2021-10-27
- Opencv note 21 frequency domain filtering
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- byte alignment
- Problems encountered when MySQL saves CSV files
- Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- 2020-08-23
猜你喜欢

Yocto technology sharing phase IV: customize and add software package support

My notes on intelligent charging pile development (II): overview of system hardware circuit design

In third tier cities and counties, it is difficult to get 10K after graduation

el-table X轴方向(横向)滚动条默认滑到右边

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

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

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

LeetCode - 919. 完全二叉树插入器 (数组)

Vgg16 migration learning source code

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
随机推荐
Installation and removal of MySQL under Windows
(1) What is a lambda expression
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
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
LeetCode - 933 最近的请求次数
Dictionary tree prefix tree trie
Tensorflow2.0 save model
Cases of OpenCV image enhancement
Positive and negative sample division and architecture understanding in image classification and target detection
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
Toolbutton property settings
Opencv gray histogram, histogram specification
我想各位朋友都应该知道学习的基本规律就是:从易到难
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Yocto technology sharing phase IV: customize and add software package support
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Stm32f407 key interrupt
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network