[Leetcode] Problem 460 - LFU Cache

Design and implement a data structure for Least Frequently Used (LFU) cache.

Implement the LFUCache class:

  • LFUCache(int capacity) Initializes the object with the capacity of the data structure.
  • int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
  • void put(int key, int value) Sets or inserts 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 this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be evicted.

Notice 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.

Follow up

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

Example

Input
[“LFUCache”, “put”, “put”, “get”, “put”, “get”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]

Output
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]

Explanation

1
2
3
4
5
6
7
8
9
10
11
LFUCache lFUCache = new LFUCache(2);
lFUCache.put(1, 1);
lFUCache.put(2, 2);
lFUCache.get(1); // return 1
lFUCache.put(3, 3); // evicts key 2
lFUCache.get(2); // return -1 (not found)
lFUCache.get(3); // return 3
lFUCache.put(4, 4); // evicts key 1.
lFUCache.get(1); // return -1 (not found)
lFUCache.get(3); // return 3
lFUCache.get(4); // return 4

Constraints

  • 0 <= capacity, key, value <= 10^4
  • At most 10^5 calls will be made to get and put.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public class LinkedNode {
private int key;
private int value;
private int frequency;
private LinkedNode next;
private LinkedNode prev;

public LinkedNode(int key, int value) {
this.key = key;
this.value = value;
this.frequency = 1;
}
}

public class LinkedNodeList {
private LinkedNode head;
private LinkedNode tail;
private int size;

public LinkedNodeList() {
head = new LinkedNode(-1, -1);
tail = new LinkedNode(-1, -1);
head.next = tail;
tail.prev = head;
}

public void add(LinkedNode node) {
node.next = head.next;
head.next.prev = node;
head.next = node;
node.prev = head;
size++;
}

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

public LinkedNode removeLast() {
LinkedNode last = tail.prev;
remove(last);
return last;
}
}

private int capacity;
private int min;
private Map<Integer, LinkedNode> map;
private Map<Integer, LinkedNodeList> freq;

public LFUCache(int capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
this.freq = new HashMap<>();
}

public int get(int key) {
if (!map.containsKey(key))
return -1;

LinkedNode node = map.get(key);
touch(node);
return node.value;
}

public void put(int key, int value) {
if (capacity == 0)
return;

LinkedNode node;

if (map.containsKey(key)) {
node = map.get(key);
node.value = value;
touch(node);
} else {
if (map.size() == capacity) {
LinkedNodeList lastList = freq.get(min);
LinkedNode last = lastList.removeLast();
map.remove(last.key);
}

min = 1;
node = new LinkedNode(key, value);
freq.computeIfAbsent(1, l -> new LinkedNodeList()).add(node);
map.put(key, node);
}
}

private void touch(LinkedNode node) {
LinkedNodeList oldList = freq.get(node.frequency);
oldList.remove(node);

if (min == node.frequency && oldList.size == 0)
min++;

node.frequency += 1;
LinkedNodeList newList = freq.getOrDefault(node.frequency, new LinkedNodeList());
newList.add(node);
freq.put(node.frequency, newList);
}