-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0146-lru-cache.cpp
127 lines (108 loc) · 3.02 KB
/
0146-lru-cache.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
146. LRU Cache
Submitted: November 30, 2024
Runtime: 67 ms (beats 85.54%)
Memory: 173.16 MB (beats 55.15%)
A solution for this problem using the std::list class rather than a custom
LinkedList class is contained within `0146-lru-cache-stl.cpp`.
*/
class LinkedList {
public:
struct Node {
int key;
int val;
Node* prev;
Node* next;
Node(int key, int val, Node* prev, Node* next) : key(key), val(val), prev(prev), next(next) {}
inline Node(int key, int val) : Node(key, val, nullptr, nullptr) {}
};
typedef unsigned short size_type;
private:
Node* const head = new Node(-1, -1);
Node* const tail = new Node(-2, -2);
size_type currentSize = 0;
inline void validateNode(Node* node) const {
if (node == nullptr) throw invalid_argument("Node cannot be null");
if (node == head || node == tail) throw invalid_argument("Cannot modify head/tail node");
}
public:
LinkedList() {
head->next = tail;
tail->prev = head;
}
Node* remove(Node* node) {
validateNode(node);
Node *prev = node->prev, *next = node->next;
prev->next = next;
next->prev = prev;
currentSize--;
return node;
}
inline void removeAndDelete(Node* node) {
validateNode(node);
delete remove(node);
}
inline Node* addToHead(int key, int val) {
Node* newNode = new Node(key, val, head, head->next);
addToHead(newNode);
return newNode;
}
void addToHead(Node* node) {
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
currentSize++;
}
inline void moveToHead(Node* node) {
validateNode(node);
// cout << node->val << endl;
remove(node);
addToHead(node);
}
inline void removeAndDeleteFromTail() {
removeAndDelete(tail->prev);
}
inline void removeFromTail() {
remove(tail->prev);
}
size_type size() const {
return currentSize;
}
Node* back() const {
return tail->prev;
}
};
class LRUCache {
private:
const LinkedList::size_type capacity;
unordered_map<int, LinkedList::Node*> map;
LinkedList list;
public:
LRUCache(int capacity) : capacity((LinkedList::size_type) capacity) {}
int get(int key) {
if (map.count(key)) {
list.moveToHead(map[key]);
return map[key]->val;
}
return -1;
}
void put(int key, int value) {
if (map.count(key)) {
list.moveToHead(map[key]);
map[key]->val = value;
} else {
if (list.size() == capacity) {
map.erase(list.back()->key);
list.removeAndDeleteFromTail();
}
map[key] = list.addToHead(key, value);
}
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/