|
| 1 | +<br> |
| 2 | +<detailsclass="hint-accordion"> |
| 3 | +<summary>Recommended Time & Space Complexity</summary> |
| 4 | +<p> |
| 5 | +You should aim for a solution with <code>O(1)</code> time for each <code>put()</code> and <code>get()</code> function call and an overall space of <code>O(n)</code>, where <code>n</code> is the capacity of the <code>LRU</code> cache. |
| 6 | +</p> |
| 7 | +</details> |
| 8 | + |
| 9 | +<br> |
| 10 | +<detailsclass="hint-accordion"> |
| 11 | +<summary>Hint 1</summary> |
| 12 | +<p> |
| 13 | +Can you think of a data structure for storing data in key-value pairs? Maybe a hash-based data structure with unique keys. |
| 14 | +</p> |
| 15 | +</details> |
| 16 | + |
| 17 | +<br> |
| 18 | +<detailsclass="hint-accordion"> |
| 19 | +<summary>Hint 2</summary> |
| 20 | +<p> |
| 21 | +We can use a hash map which takes <code>O(1)</code> time to get and put the values. But, how can you deal with the least recently used to be removed criteria as a key is updated by the <code>put()</code> or recently used by the <code>get()</code> functions? Can you think of a data structure to store the order of values? |
| 22 | +</p> |
| 23 | +</details> |
| 24 | + |
| 25 | +<br> |
| 26 | +<detailsclass="hint-accordion"> |
| 27 | +<summary>Hint 3</summary> |
| 28 | +<p> |
| 29 | +A brute-force solution would involve maintaining the order of key-value pairs in an array list, performing operations by iterating through the list to erase and insert these key-value pairs. However, this would result in an <code>O(n)</code> time complexity. Can you think of a data structure that allows removing and reinserting elements in <code>O(1)</code> time? |
| 30 | +</p> |
| 31 | +</details> |
| 32 | + |
| 33 | +<br> |
| 34 | +<detailsclass="hint-accordion"> |
| 35 | +<summary>Hint 4</summary> |
| 36 | +<p> |
| 37 | +We can use a doubly-linked list, which allows us to remove a node from the list when we have the address of that node. Can you think of a way to store these addresses so that we can efficiently remove or update a key when needed? |
| 38 | +</p> |
| 39 | +</details> |
| 40 | + |
| 41 | +<br> |
| 42 | +<detailsclass="hint-accordion"> |
| 43 | +<summary>Hint 5</summary> |
| 44 | +<p> |
| 45 | +We can use a doubly linked list where key-value pairs are stored as nodes, with the least recently used (LRU) node at the head and the most recently used (MRU) node at the tail. Whenever a key is accessed using <code>get()</code> or <code>put()</code>, we remove the corresponding node and reinsert it at the tail. When the cache reaches its capacity, we remove the LRU node from the head of the list. Additionally, we use a hash map to store each key and the corresponding address of its node, enabling efficient operations in <code>O(1)</code> time. |
| 46 | +</p> |
| 47 | +</details> |