Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. class LRUCache {
  2. public:
  3.    
  4.     struct Node {
  5.         int val;
  6.         int key;
  7.         struct Node *prev;
  8.         struct Node *next;
  9.     };
  10.    
  11.     // key, Node
  12.     unordered_map<int, Node*> hash;
  13.     Node *head;
  14.     Node *tail;
  15.     int cap;
  16.    
  17.    
  18.     LRUCache(int capacity) {
  19.         head = new Node;
  20.         tail = new Node;
  21.         head->next = tail;
  22.         tail->prev = head;
  23.         head->prev = NULL;
  24.         tail->next = NULL;
  25.         cap = capacity;
  26.     }
  27.    
  28.     ~LRUCache(){
  29.         Node *ptr = head;
  30.         while (ptr != NULL){
  31.             Node *tmp = ptr->next;
  32.             delete ptr;
  33.             ptr = tmp;
  34.         }
  35.     }
  36.    
  37.     /* void status(){
  38.         Node *ptr = head->next;
  39.         cout << "LL Front: ";
  40.         while (ptr != tail){
  41.             cout << ptr->key << "->";
  42.             ptr = ptr->next;
  43.         }
  44.         cout << "\tLL Back: ";
  45.         ptr = tail->prev;
  46.         while (ptr != head){
  47.             cout << "<-" << ptr->key;
  48.             ptr = ptr->prev;
  49.         }
  50.         cout << "\n";
  51.     } */
  52.    
  53.     int get(int key) {
  54.         if (hash.find(key) != hash.end()){
  55.             Node *res = hash.find(key)->second;
  56.            
  57.             // move to front
  58.             res->prev->next = res->next;
  59.             res->next->prev = res->prev;
  60.             res->prev = head;
  61.             res->next = head->next;
  62.             head->next->prev = res;
  63.             head->next = res;
  64.             return res->val;
  65.         } else {
  66.             return -1;
  67.         }
  68.     }
  69.    
  70.     void put(int key, int value) {        
  71.         Node *res;
  72.         if (hash.find(key) == hash.end()){
  73.             res = new Node;
  74.             res->val = value;
  75.             res->key = key;
  76.             res->prev = head;
  77.             res->next = head->next;
  78.             res->next->prev = res;
  79.             head->next = res;
  80.             hash.insert({key, res});
  81.             cap--;
  82.         } else {
  83.             res = hash.find(key)->second;
  84.             res->val = value;
  85.            
  86.             // move to front
  87.             res->prev->next = res->next;
  88.             res->next->prev = res->prev;
  89.             res->prev = head;
  90.             res->next = head->next;
  91.             head->next->prev = res;
  92.             head->next = res;
  93.         }
  94.        
  95.         if (cap < 0){
  96.             // invalidate last one if out of capacity
  97.             Node* inv = tail->prev;
  98.             inv->prev->next = tail;
  99.             tail->prev = inv->prev;
  100.             hash.erase(inv->key);
  101.             delete inv;
  102.             cap++;
  103.         }
  104.     }
  105. };
  106.  
  107. /**
  108.  * Your LRUCache object will be instantiated and called as such:
  109.  * LRUCache* obj = new LRUCache(capacity);
  110.  * int param_1 = obj->get(key);
  111.  * obj->put(key,value);
  112.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement