jayati

LRU Cache

Oct 8th, 2023
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. // } Driver Code Ends
  7. // design the class in the most optimal way
  8.  
  9. class LRUCache
  10. {
  11.     private:
  12.     class Node {
  13.         public:
  14.         int key;
  15.         int val;
  16.         Node *prev, *next;
  17.        
  18.         Node(int k, int v) {
  19.             key = k;
  20.             val = v;
  21.             prev = next = nullptr;
  22.         }
  23.     };
  24.    
  25.     Node *head = new Node(-1, -1);
  26.     Node *tail = new Node(-1, -1);
  27.     unordered_map<int, Node*> mp;
  28.     int size;
  29.    
  30.     void addNode(Node *ptr) {
  31.         ptr->prev = head;
  32.         ptr->next = head->next;
  33.         head->next->prev = ptr;
  34.         head->next = ptr;
  35.     }
  36.    
  37.     void deleteNode(Node *ptr) {
  38.         Node *next_of_ptr = ptr->next;
  39.         Node *prev_of_ptr = ptr->prev;
  40.        
  41.         prev_of_ptr->next = next_of_ptr;
  42.         next_of_ptr->prev = prev_of_ptr;
  43.        
  44.         delete ptr;
  45.     }
  46.    
  47.     public:
  48.    
  49.     LRUCache(int cap)
  50.     {
  51.         size = cap;
  52.         head->next = tail;
  53.         tail->prev = head;
  54.     }
  55.    
  56.     int GET(int key)
  57.     {
  58.         if(mp.find(key) != mp.end()) {
  59.             Node* resnode = mp[key];
  60.             int res = resnode->val;
  61.             deleteNode(resnode);
  62.  
  63.             Node *newNode = new Node(key, res);
  64.             addNode(newNode);
  65.             mp[key] = newNode;
  66.            
  67.             return res;
  68.         }
  69.         return -1;
  70.     }
  71.    
  72.     void SET(int key, int value)
  73.     {
  74.         if(mp.find(key) != mp.end()) {
  75.             Node* resnode = mp[key];
  76.             deleteNode(resnode);
  77.         }
  78.         else if(mp.size() == size) {
  79.             mp.erase(tail->prev->key);
  80.             deleteNode(tail->prev);
  81.         }
  82.        
  83.         Node *newNode = new Node(key, value);
  84.         addNode(newNode);
  85.         mp[key] = newNode;
  86.     }
  87. };
  88.  
  89. //{ Driver Code Starts.
  90.  
  91. int main()
  92. {
  93.     int t;
  94.     cin >> t;
  95.     while (t--)
  96.     {
  97.  
  98.         int capacity;
  99.         cin >> capacity;
  100.         LRUCache *cache = new LRUCache(capacity);
  101.        
  102.         int queries;
  103.         cin >> queries;
  104.         while (queries--)
  105.         {
  106.             string q;
  107.             cin >> q;
  108.             if (q == "SET")
  109.             {
  110.                 int key;
  111.                 cin >> key;
  112.                 int value;
  113.                 cin >> value;
  114.                 cache->SET(key, value);
  115.             }
  116.             else
  117.             {
  118.                 int key;
  119.                 cin >> key;
  120.                 cout << cache->GET(key) << " ";
  121.             }
  122.         }
  123.         cout << endl;
  124.     }
  125.     return 0;
  126. }
  127.  
  128. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment