CT017

LRU Memory Vault

HardAcceptance: 0.0%

The Codetikki archivists maintain a memory vault of fixed capacity. When the vault is full and a new memory must be stored, the least recently accessed memory is evicted. Implement an LRU cache with capacity n supporting: get key (returns value or -1 if not present), put key value (sets key to value, evicting LRU if at capacity). Each operation must run in O(1) time. Input: first line capacity n, second line number of operations m, then m lines each like "get k" or "put k v". Output: results of get operations, one per line.

Example 1:

Input: 2 6 put 1 1 put 2 2 get 1 put 3 3 get 2 get 3
Output: 1 -1 3

Example 2:

Input: 1 3 put 1 10 get 1 put 2 20
Output: 10

Constraints:

1 <= capacity <= 10^4 1 <= m <= 10^5 0 <= key, value <= 10^9

Tags:

hash-table linked-list design data-structures
Loading...
Test Cases:No test cases
No test cases available.
Coding Problem Not Found | CodeTikki