-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146-LRU-Cache.cs
More file actions
113 lines (99 loc) · 2.8 KB
/
Copy path146-LRU-Cache.cs
File metadata and controls
113 lines (99 loc) · 2.8 KB
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
// Create a class called double linked node that has references to next and previous nodes
// Add pseudo head and tail values to the linkedList to make it easier to add and remove nodes
// Add DoubleLinkedNode helper methods such as:
// addNode (always adds to front)
// removeNode (removes a specific node object from the list)
// popTail (removes last node and returns the node object)
// moveToFront (calls removeNode() on the node object and then addNode() ot add node back to front)
// DOUBLE LINKED LIST + HASH MAP Solution
// O(1) Time, O(capacity) Space
public class LRUCache
{
public class DoubleLinkedNode
{
public int key;
public int value;
public DoubleLinkedNode prev;
public DoubleLinkedNode next;
}
private void addNode(DoubleLinkedNode node)
{
_head.next.prev = node;
node.next = _head.next;
_head.next = node;
node.prev = _head;
}
private void removeNode(DoubleLinkedNode node)
{
node.prev.next = node.next;
node.next.prev = node.prev;
}
private DoubleLinkedNode popTail()
{
var lastNode = _tail.prev;
removeNode(lastNode);
return lastNode;
}
private void moveToFront(DoubleLinkedNode node)
{
removeNode(node);
addNode(node);
}
private DoubleLinkedNode _head;
private DoubleLinkedNode _tail;
private int _capacity;
private int _count;
private Dictionary<int, DoubleLinkedNode> dict;
public LRUCache(int capacity)
{
_head = new DoubleLinkedNode();
_tail = new DoubleLinkedNode();
_head.next = _tail;
_tail.prev = _head;
_capacity = capacity;
_count = 0;
dict = new Dictionary<int, DoubleLinkedNode>();
}
public int Get(int key)
{
if (dict.ContainsKey(key))
{
var node = dict[key];
moveToFront(node);
return node.value;
}
else
{
return -1;
}
}
public void Put(int key, int value)
{
if (!dict.ContainsKey(key))
{
var newNode = new DoubleLinkedNode();
newNode.key = key;
newNode.value = value;
addNode(newNode);
dict[key] = newNode;
if (dict.Count > _capacity)
{
var removedNode = popTail();
dict.Remove(removedNode.key);
Console.WriteLine(dict.Count);
}
}
else
{
var existingNode = dict[key];
moveToFront(existingNode);
existingNode.value = 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);
*/