350. LRU Cache
MediumDesignLinked Hash Map
Simulate an LRU cache of capacity k: get x prints the value for key x or -1 (and marks it recently used); put x v sets the value, evicting the least recently used key when full.
Input format
Line 1: k q Next q lines: commands
Output format
One line per get.
Example 1
Input
2 9 put 1 1 put 2 2 get 1 put 3 3 get 2 put 4 4 get 1 get 3 get 4
Output
1 -1 -1 3 4
Example 2
Input
1 3 put 2 1 get 2 get 3
Output
1 -1
Constraints
1 ≤ k ≤ 1000 1 ≤ q ≤ 10⁵
Read input from standard input and print the answer to standard output. Your code is judged against 3 test cases, most of them hidden.
Loading the MyLoginn IDE…