fork download
  1. // Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
  2.  
  3. // Implement the LRUCache class:
  4.  
  5. // LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  6. // int get(int key) Return the value of the key if the key exists, otherwise return -1.
  7. // void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
  8. // The functions get and put must each run in O(1) average time complexity.
  9.  
  10.  
  11.  
  12. // Example 1:
  13.  
  14. // Input
  15. // ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
  16. // [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
  17. // Output
  18. // [null, null, null, 1, null, -1, null, -1, 3, 4]
  19.  
  20. // Explanation
  21. // LRUCache lRUCache = new LRUCache(2);
  22. // lRUCache.put(1, 1); // cache is {1=1}
  23. // lRUCache.put(2, 2); // cache is {1=1, 2=2}
  24. // lRUCache.get(1); // return 1
  25. // lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
  26. // lRUCache.get(2); // returns -1 (not found)
  27. // lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
  28. // lRUCache.get(1); // return -1 (not found)
  29. // lRUCache.get(3); // return 3
  30. // lRUCache.get(4); // return 4
  31.  
  32. #include <iostream>
  33. using namespace std;
  34.  
  35. int main() {
  36. // your code goes here
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty