Skip to content

Commit 9f1bcc2

Browse files
committed
update
1 parent 50823b9 commit 9f1bcc2

File tree

1 file changed

+61
-32
lines changed

1 file changed

+61
-32
lines changed

Diff for: 146.LRUCache.cs

+61-32
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,82 @@
2727
// lRUCache.get(3); // return 3
2828
// lRUCache.get(4); // return 4
2929

30+
//Using LinkList
31+
public class LRUCache
32+
{
33+
// Define the capacity of the LRU Cache
34+
private readonly int capacity;
35+
36+
// Dictionary to store the cache items with key as the integer key and value as the LinkedListNode containing a tuple (key, value)
37+
private readonly Dictionary<int, LinkedListNode<(int key, int value)>> cache;
38+
39+
// LinkedList to maintain the order of usage, with the most recently used item at the front
40+
private readonly LinkedList<(int key, int value)> lruList;
3041

31-
//
32-
public class LRUCache {
33-
private readonly int _capacity;
34-
private readonly Dictionary<int, LinkedListNode<KeyValuePair<int, int>>> _cache;
35-
private readonly LinkedList<KeyValuePair<int, int>> _lru;
36-
37-
public LRUCache(int capacity)
42+
// Constructor to initialize the LRU Cache with a given capacity
43+
public LRUCache(int capacity)
3844
{
39-
_capacity = capacity;
40-
_cache = new();
41-
_lru = new();
45+
this.capacity = capacity;
46+
cache = new Dictionary<int, LinkedListNode<(int key, int value)>>();
47+
lruList = new LinkedList<(int key, int value)>();
4248
}
43-
44-
public int Get(int key)
49+
50+
// Method to get the value associated with a key
51+
public int Get(int key)
4552
{
46-
if (!_cache.TryGetValue(key, out var node))
53+
// Check if the key exists in the cache
54+
if (cache.TryGetValue(key, out LinkedListNode<(int key, int value)> node))
4755
{
48-
return -1;
56+
// If key exists, move the node to the front of the LinkedList (most recently used)
57+
lruList.Remove(node);
58+
lruList.AddFirst(node);
59+
// Return the value associated with the key
60+
return node.Value.value;
4961
}
50-
51-
_lru.Remove(node);
52-
_cache[key] = _lru.AddFirst(node.Value);
53-
54-
return node.Value.Value;
62+
// If key does not exist, return -1
63+
return -1; // Key not found
5564
}
56-
57-
public void Put(int key, int value)
65+
66+
// Method to put a key-value pair into the cache
67+
public void Put(int key, int value)
5868
{
59-
if (_cache.TryGetValue(key, out var node))
69+
// Check if the key already exists in the cache
70+
if (cache.TryGetValue(key, out LinkedListNode<(int key, int value)> node))
6071
{
61-
_lru.Remove(node);
62-
_cache.Remove(key);
72+
// If key exists, remove the node from its current position
73+
lruList.Remove(node);
74+
// Update the value of the node
75+
node.Value = (key, value);
76+
// Add the node to the front of the LinkedList (most recently used)
77+
lruList.AddFirst(node);
6378
}
64-
65-
node = _lru.AddFirst(new KeyValuePair<int, int>(key, value));
66-
_cache[key] = node;
67-
68-
if (_lru.Count > _capacity)
79+
else
6980
{
70-
node = _lru.Last;
71-
_lru.RemoveLast();
72-
_cache.Remove(node.Value.Key);
81+
// If key does not exist and cache is at full capacity
82+
if (cache.Count >= capacity)
83+
{
84+
// Remove the least recently used item from the LinkedList and the cache
85+
var lruNode = lruList.Last;
86+
lruList.RemoveLast();
87+
cache.Remove(lruNode.Value.key);
88+
}
89+
// Create a new node for the key-value pair
90+
var newNode = new LinkedListNode<(int key, int value)>((key, value));
91+
// Add the new node to the front of the LinkedList (most recently used)
92+
lruList.AddFirst(newNode);
93+
// Add the new node to the cache
94+
cache[key] = newNode;
7395
}
7496
}
7597
}
7698

99+
/**
100+
* Your LRUCache object will be instantiated and called as such:
101+
* LRUCache obj = new LRUCache(capacity);
102+
* int param_1 = obj.Get(key);
103+
* obj.Put(key,value);
104+
*/
105+
77106
//self defined DLL
78107
public class LRUCache {
79108
public class DoublyLinkList

0 commit comments

Comments
 (0)