当前位置:网站首页>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); */
边栏推荐
- Notes on C language learning of migrant workers majoring in electronic information engineering
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- Simulate mouse click
- getopt_ Typical use of long function
- Qcombox style settings
- Drive and control program of Dianchuan charging board for charging pile design
- Opencv gray histogram, histogram specification
- An executable binary file contains more than machine instructions
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
猜你喜欢
el-table X轴方向(横向)滚动条默认滑到右边
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode - 919. 完全二叉树插入器 (数组)
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
My notes on intelligent charging pile development (II): overview of system hardware circuit design
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Basic knowledge of communication interface
2021-10-27
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
03 fastjason solves circular references
随机推荐
QT setting suspension button
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
(1) 什么是Lambda表达式
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Opencv histogram equalization
I think all friends should know that the basic law of learning is: from easy to difficult
4G module initialization of charge point design
pycharm 无法引入自定义包
When the reference is assigned to auto
01 business structure of imitation station B project
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
Swing transformer details-2
QT self drawing button with bubbles
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Tensorflow2.0 save model
Positive and negative sample division and architecture understanding in image classification and target detection
Qcombox style settings
Retinaface: single stage dense face localization in the wild
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Notes on C language learning of migrant workers majoring in electronic information engineering