Formatted question description: https://leetcode.ca/all/460.html

# 460. LFU Cache

Hard

## Description

Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put.

• get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
• put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.

Note that the number of times an item is used is the number of calls to the get and put functions for that item since it was inserted. This number is set to zero when the item is removed.

Could you do both operations in O(1) time complexity?

Example:

LFUCache cache = new LFUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(3);       // returns 3.
cache.put(4, 4);    // evicts key 1.
cache.get(3);       // returns 3
cache.get(4);       // returns 4


## Solution

Create two classes Node and DoublyLinkedList. Each object of Node has a key, a value, a frequency, the previous node and the next node. Each object of DoublyLinkedList has a size, a head and a tail.

In class LFUCache, maintain two maps cache and frequencyMap, which stores each key and the corresponding node, and stores each frequency and the corresponding doubly linked lists, respectively. Also maintain size, capacity and minCapacity.

For the constructor, initialize the two maps and the capacity.

For method get, get the node from cache. If the node exists, update the node’s frequency and return the node’s value. Otherwise, return -1.

For method put, check whether the node of the key already exists. If the node already exists, update the node’s value and update the node’s frequency. Otherwise, if size already reaches compacity, obtain the minimum frequency list using minFrequency and remove the last node, and decreasesize by 1. Then create a new node using key and value, update the maps, increase size by 1 and set minFrequency = 1.

Java

class LFUCache {
Map<Integer, Node> cache;
int size;
int capacity;
int minFrequency;

public LFUCache(int capacity) {
cache = new HashMap<Integer, Node>();
this.capacity = capacity;
}

public int get(int key) {
Node node = cache.get(key);
if (node == null)
return -1;
else {
increaseFrequency(node);
return node.value;
}
}

public void put(int key, int value) {
if (capacity == 0)
return;
Node node = cache.get(key);
if (node == null) {
if (size == capacity) {
Node removeNode = minList.tail.prev;
cache.remove(removeNode.key);
minList.remove(removeNode);
size--;
}
Node newNode = new Node(key, value);
cache.put(key, newNode);
if (list == null) {
frequencyMap.put(1, list);
}
size++;
minFrequency = 1;
} else {
node.value = value;
increaseFrequency(node);
}
}

private void increaseFrequency(Node node) {
int frequency = node.frequency;
prevList.remove(node);
if (frequency == minFrequency && prevList.size == 0)
minFrequency = frequency + 1;
node.frequency++;
DoublyLinkedList newList = frequencyMap.get(frequency + 1);
if (newList == null) {
frequencyMap.put(frequency + 1, newList);
}
}
}

class Node {
int key;
int value;
int frequency = 1;
Node prev;
Node next;

public Node() {

}

public Node(int key, int value) {
this.key = key;
this.value = value;
}
}

int size;
Node tail;

size = 0;
tail = new Node();
}

public void remove(Node node) {
size--;
node.prev.next = node.next;
node.next.prev = node.prev;
}

size++;
}
}

/**
* 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);
*/