leetcode 460 - LFU Cache

https://leetcode.com/problems/lfu-cache/

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.

Follow up:
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(2); // returns -1 (not found)
cache.get(3); // returns 3.
cache.put(4, 4); // evicts key 1.
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4

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
104
105
106
107
class LFUCache {
public:
struct Node {
int key;
int val;
int count;
Node *prev, *next;

Node (int k) {
key = k;
count = 0;
prev = next = NULL;
}
};

map<int, Node *> freqMap;
unordered_map<int, Node *> keyValue;
int capacity;

LFUCache(int capacity) {
this->capacity = capacity;
}

int get(int key) {
if (keyValue.count(key) == 0)
return -1;
Node *node = keyValue[key];
remove(node);
node->count++;
insert(node);
return node->val;
}

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

Node *node;
if (keyValue.count(key) == 0) {
if ((int)keyValue.size() == capacity) {
evict();
}
node = new Node(key);
} else {
node = keyValue[key];
remove(node);
}

node->count++;
node->val = value;
insert(node);
}

void remove(Node *node) {
int count = node->count;

Node *prev = node->prev;
Node *next = node->next;
prev->next = next;
next->prev = prev;

Node *head = freqMap[count];
if (head == node) {
if (prev == node)
freqMap.erase(count);
else
freqMap[count] = next;
}

int key = node->key;
keyValue.erase(key);
}

void insert(Node *node) {
int count = node->count;

Node *head = freqMap[count];
if (head) {
node->next = head;
Node *tail = head->prev;
node->prev = tail;
head->prev = node;
tail->next = node;
} else {
node->prev = node;
node->next = node;
}

freqMap[count] = node;
keyValue[node->key] = node;
}

void evict() {
auto it = freqMap.begin();
Node *head = it->second;
Node *tail = head->prev;
remove(tail);
delete tail;
}
};

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