6
Map<Integer, DLLNode> cache;7
Map<Integer, DoubleLinkedList> frequencyMap;11
* @param capacity: total capacity of LFU Cache12
* @param curSize: current size of LFU cache13
* @param minFrequency: frequency of the last linked list (the minimum frequency of entire LFU cache)14
* @param cache: a hash map that has key to Node mapping, which used for storing all nodes by their keys15
* @param frequencyMap: a hash map that has key to linked list mapping, which used for storing all16
* double linked list by their frequencies18
public LFUCache(int capacity) {19
this.capacity = capacity;21
this.minFrequency = 0;23
this.cache = new HashMap<>();24
this.frequencyMap = new HashMap<>();27
/** get node value by key, and then update node frequency as well as relocate that node * */28
public int get(int key) {29
DLLNode curNode = cache.get(key);30
if (curNode == null) {38
* add new node into LFU cache, as well as double linked list condition 1: if LFU cache has input39
* key, update node value and node position in list condition 2: if LFU cache does NOT have input40
* key - sub condition 1: if LFU cache does NOT have enough space, remove the Least Recent Used41
* node in minimum frequency list, then add new node - sub condition 2: if LFU cache has enough42
* space, add new node directly *44
public void put(int key, int value) {45
// corner case: check cache capacity initialization50
if (cache.containsKey(key)) {51
DLLNode curNode = cache.get(key);56
if (curSize > capacity) {57
// get minimum frequency list58
DoubleLinkedList minFreqList = frequencyMap.get(minFrequency);59
cache.remove(minFreqList.tail.prev.key);60
minFreqList.removeNode(minFreqList.tail.prev);63
// reset min frequency to 1 because of adding new node65
DLLNode newNode = new DLLNode(key, value);67
// get the list with frequency 1, and then add new node into the list, as well as into LFU69
DoubleLinkedList curList = frequencyMap.getOrDefault(1, new DoubleLinkedList());70
curList.addNode(newNode);71
frequencyMap.put(1, curList);72
cache.put(key, newNode);76
public void updateNode(DLLNode curNode) {77
int curFreq = curNode.frequency;78
DoubleLinkedList curList = frequencyMap.get(curFreq);79
curList.removeNode(curNode);81
// if current list the the last list which has lowest frequency and current node is the only83
// we need to remove the entire list and then increase min frequency value by 184
if (curFreq == minFrequency && curList.listSize == 0) {89
// add current node to another list has current frequency + 1,90
// if we do not have the list with this frequency, initialize it91
DoubleLinkedList newList = frequencyMap.getOrDefault(curNode.frequency, new DoubleLinkedList());92
newList.addNode(curNode);93
frequencyMap.put(curNode.frequency, newList);97
* @param key: node key98
* @param val: node value99
* @param frequency: frequency count of current node100
* (all nodes connected in same double linked list has same frequency)101
* @param prev: previous pointer of current node102
* @param next: next pointer of current node111
public DLLNode(int key, int val) {119
* @param listSize: current size of double linked list120
* @param head: head node of double linked list121
* @param tail: tail node of double linked list123
class DoubleLinkedList {128
public DoubleLinkedList() {130
this.head = new DLLNode(0, 0);131
this.tail = new DLLNode(0, 0);136
/** add new node into head of list and increase list size by 1 * */137
public void addNode(DLLNode curNode) {138
DLLNode nextNode = head.next;139
curNode.next = nextNode;142
nextNode.prev = curNode;146
/** remove input node and decrease list size by 1* */147
public void removeNode(DLLNode curNode) {148
DLLNode prevNode = curNode.prev;149
DLLNode nextNode = curNode.next;150
prevNode.next = nextNode;151
nextNode.prev = prevNode;158
* Your LFUCache object will be instantiated and called as such: LFUCache obj = new159
* LFUCache(capacity); int param_1 = obj.get(key); obj.put(key,value);