Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. // Runtime: 61 ms, faster than 65.05% of Java online submissions for LRU Cache.
  2. // Memory Usage: 57.5 MB, less than 61.35% of Java online submissions for LRU Cache.
  3. class LRUCache {
  4.     Map<Integer, CacheItem> map;
  5.     CacheItem head;
  6.     CacheItem tail;
  7.     int capacity;
  8.    
  9.     public LRUCache(int capacity) {
  10.         this.capacity = capacity;
  11.         map = new HashMap<Integer, CacheItem>();
  12.         head = null;
  13.         tail = null;
  14.     }
  15.    
  16.     public int get(int key) {
  17.         CacheItem item = map.get(key);
  18.         if (item == null) return -1;
  19.        
  20.         use(item);
  21.        
  22.         return item.val;
  23.     }
  24.    
  25.     private void use(CacheItem item) {        
  26.         if (head == tail) return;
  27.         if (tail == item) return;
  28.        
  29.         if (item == head) setHead(item.next);
  30.         else item.prev.linkNext(item.next);
  31.        
  32.         setTail(item);        
  33.     }
  34.    
  35.     public void put(int key, int value) {        
  36.         CacheItem item = map.get(key);
  37.        
  38.         if (item != null) {
  39.             item.val = value;
  40.             use(item);
  41.             return;
  42.         }
  43.        
  44.         if (map.size() == capacity) evict();        
  45.        
  46.         item = new CacheItem(key, value);
  47.         map.put(key, item);
  48.        
  49.         if (head == null) setHead(item);
  50.        
  51.         setTail(item);      
  52.     }    
  53.    
  54.     private void evict() {
  55.         map.remove(head.key);
  56.        
  57.         if (head == tail) tail = null;        
  58.         setHead(head.next);        
  59.     }
  60.    
  61.     private void setHead(CacheItem item) {
  62.         head = item;
  63.         if (head != null) head.prev = null;
  64.     }
  65.    
  66.     private void setTail(CacheItem item) {
  67.         if (tail == item) return;
  68.        
  69.         if (tail != null) {
  70.             tail.linkNext(item);
  71.             item.next = null;
  72.         }
  73.         tail = item;
  74.     }
  75.    
  76.     class CacheItem {
  77.         public int key;
  78.        
  79.         public int val;
  80.        
  81.         public CacheItem next;
  82.        
  83.         public CacheItem prev;
  84.        
  85.         public CacheItem(int key, int value){
  86.             this.key = key;
  87.             this.val = value;
  88.         }
  89.        
  90.         public void linkNext(CacheItem n) {
  91.             next = n;
  92.             if (n != null) n.prev = this;
  93.         }
  94.     }
  95. }
  96.  
  97. /**
  98.  * Your LRUCache object will be instantiated and called as such:
  99.  * LRUCache obj = new LRUCache(capacity);
  100.  * int param_1 = obj.get(key);
  101.  * obj.put(key,value);
  102.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement