SHARE
TWEET

Untitled

a guest Aug 20th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
  2.  
  3. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
  4. put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
  5.  
  6. The cache is initialized with a positive capacity.
  7.  
  8. Follow up:
  9. Could you do both operations in O(1) time complexity?
  10.  
  11. Example:
  12.  
  13. LRUCache cache = new LRUCache( 2 /* capacity */ );
  14.  
  15. cache.put(1, 1);
  16. cache.put(2, 2);
  17. cache.get(1);       // returns 1
  18. cache.put(3, 3);    // evicts key 2
  19. cache.get(2);       // returns -1 (not found)
  20. cache.put(4, 4);    // evicts key 1
  21. cache.get(1);       // returns -1 (not found)
  22. cache.get(3);       // returns 3
  23. cache.get(4);       // returns 4
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top