Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6793711

Browse files
authored
Merge pull request #1 from kiryl-labada/update-csharp-snippets
Update 0146-lru-cache.cs
2 parents db263fc + a2e4bbc commit 6793711

File tree

1 file changed

+26
-79
lines changed

1 file changed

+26
-79
lines changed

csharp/0146-lru-cache.cs

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,39 @@
1-
public class Node
2-
{
3-
public int val;
4-
public int key;
5-
public Node prev;
6-
public Node next;
1+
public class LRUCache {
2+
private Dictionary<int, LinkedListNode<(int key, int value)>> _dict = new();
3+
private LinkedList<(int key, int value)> _values = new();
74

8-
public Node(int key, int val)
9-
{
10-
this.key = key;
11-
this.val = val;
12-
prev = null;
13-
next = null;
14-
}
15-
}
5+
private int _capacity;
166

17-
public class LRUCache
18-
{
19-
private Dictionary<int, Node> keyValue = new();
20-
private int _capacity;
21-
private Node left;
22-
private Node right;
23-
24-
//T: O(1), S: O(Capacity)
25-
public LRUCache(int capacity)
26-
{
7+
public LRUCache(int capacity) {
278
_capacity = capacity;
28-
left = new Node(0, 0);
29-
right = new Node(0, 0);
30-
// left : LRU, right : MRU (Most recently used)
31-
left.next = right;
32-
right.prev = left;
339
}
10+
11+
public int Get(int key) {
12+
if (!_dict.ContainsKey(key)) {
13+
return -1;
14+
}
3415

35-
//Remove from list
36-
private void remove(Node node)
37-
{
38-
var prev = node.prev;
39-
var next = node.next;
40-
41-
prev.next = next;
42-
next.prev = prev;
43-
}
44-
45-
// Insert at right
46-
private void insert(Node node)
47-
{
48-
var next = right;
49-
var prev = right.prev;
50-
51-
node.next = next;
52-
next.prev = node;
53-
prev.next = node;
54-
node.prev = prev;
16+
var node = _dict[key];
17+
_values.Remove(node);
18+
_values.AddFirst(node);
5519

20+
return node.Value.value;
5621
}
57-
58-
public int Get(int key)
59-
{
60-
if (!keyValue.ContainsKey(key))
61-
return -1;
62-
// we need to update this to be MRU
63-
var node = keyValue[key];
64-
remove(node);
65-
insert(node);
66-
return node.val;
67-
}
68-
69-
public void Put(int key, int value)
70-
{
71-
if (keyValue.ContainsKey(key))
72-
{
73-
var node = keyValue[key];
74-
keyValue.Remove(key);
75-
remove(node);
22+
23+
public void Put(int key, int value) {
24+
if (!_dict.ContainsKey(key) && _dict.Count >= _capacity) {
25+
var node = _values.Last;
26+
_dict.Remove(node.Value.key);
27+
_values.Remove(node);
7628
}
7729

78-
var newNode = new Node(key, value);
79-
keyValue.Add(key, newNode);
80-
insert(newNode);
81-
82-
if (keyValue.Count > _capacity)
83-
{
84-
// remove from the list and delete the LRU from dictionary
85-
var lru = left.next;
86-
remove(lru);
87-
keyValue.Remove(lru.key);
30+
var existingNode = _dict.GetValueOrDefault(key);
31+
if (existingNode != null) {
32+
_values.Remove(existingNode);
8833
}
8934

35+
_values.AddFirst((key, value));
36+
_dict[key] = _values.First;
9037
}
9138
}
9239

@@ -95,4 +42,4 @@ public void Put(int key, int value)
9542
* LRUCache obj = new LRUCache(capacity);
9643
* int param_1 = obj.Get(key);
9744
* obj.Put(key,value);
98-
*/
45+
*/

0 commit comments

Comments
 (0)