Advertisement
Catsher

LRU cache leetcode

Mar 31st, 2025
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. public class LRUCache
  2. {
  3.     public readonly int _capacity;
  4.  
  5.     private int _count = 0;
  6.  
  7.     private readonly Dictionary<int, OurNode> _keyToLinkToNode;
  8.     private readonly OurLinkedList _queue = new();
  9.  
  10.     public LRUCache(int capacity)
  11.     {
  12.         _capacity = capacity;
  13.         _keyToLinkToNode = new(_capacity + 1);
  14.     }
  15.  
  16.     public int Get(int key)
  17.     {
  18.         if (!_keyToLinkToNode.TryGetValue(key, out OurNode? neededNode))
  19.         {
  20.             return -1;
  21.         }
  22.  
  23.         var getValue = neededNode.Value.Value;
  24.  
  25.         _queue.ReplaceToEnd(neededNode);
  26.  
  27.         return getValue;
  28.     }
  29.  
  30.     public void Put(int key, int value)
  31.     {
  32.         if (_keyToLinkToNode.TryGetValue(key, out OurNode existingNode))
  33.         {
  34.             existingNode.Value = KeyValuePair.Create(key, value);
  35.             _queue.ReplaceToEnd(existingNode);
  36.         }
  37.         else
  38.         {
  39.             if (_capacity != _count)
  40.             {
  41.                 _count++;
  42.             }
  43.             else
  44.             {
  45.                 var nodeToDelete = _queue.First;
  46.                 _keyToLinkToNode.Remove(nodeToDelete.Value.Key);
  47.                 _queue.RemoveFirst();
  48.             }
  49.  
  50.             _queue.AddLast(KeyValuePair.Create(key, value));
  51.             var addedNode = _queue.Last;
  52.             _keyToLinkToNode[key] = addedNode;
  53.         }
  54.     }
  55. }
  56.  
  57. internal class OurLinkedList
  58. {
  59.     public OurNode First;
  60.     public OurNode Last;
  61.  
  62.     public void ReplaceToEnd(OurNode node)
  63.     {
  64.         if (Last == node)
  65.         {
  66.             return;
  67.         }
  68.         else if (First == node)
  69.         {
  70.             First = First.Next;
  71.             First.Previous = null!;
  72.  
  73.             node.Previous = Last;
  74.             node.Next = null!;
  75.  
  76.             Last.Next = node;
  77.             Last = node;
  78.         }
  79.         else
  80.         {
  81.             node.Previous.Next = node.Next;
  82.             node.Next.Previous = node.Previous;
  83.  
  84.             node.Previous = Last;
  85.             node.Next = null!;
  86.  
  87.             Last.Next = node;
  88.             Last = node;
  89.         }
  90.     }
  91.  
  92.     public void RemoveFirst()
  93.     {
  94.         if (First == Last)
  95.         {
  96.             First = null!;
  97.             Last = null!;
  98.         }
  99.         else
  100.         {
  101.             First = First.Next;
  102.             First.Previous = null!;
  103.         }
  104.     }
  105.  
  106.     public void AddLast(KeyValuePair value)
  107.     {
  108.         OurNode newNode = new(value);
  109.  
  110.         if (Last == null)
  111.         {
  112.             First = newNode;
  113.             Last = newNode;
  114.         }
  115.         else
  116.         {
  117.             newNode.Previous = Last;
  118.             Last.Next = newNode;
  119.             Last = newNode;
  120.         }
  121.     }
  122. }
  123.  
  124. internal class OurNode
  125. {
  126.     public KeyValuePair Value { get; set; }
  127.     public OurNode Previous { get; set; }
  128.     public OurNode Next { get; set; }
  129.  
  130.     public OurNode(KeyValuePair value)
  131.     {
  132.         Value = value;
  133.     }
  134. }
  135.  
  136. internal struct KeyValuePair
  137. {
  138.     public int Key;
  139.     public int Value;
  140.  
  141.     public KeyValuePair(int key, int value)
  142.     {
  143.         Key = key;
  144.         Value = value;
  145.     }
  146.  
  147.     public static KeyValuePair Create(int key, int value) => new(key, value);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement