Movie LRU Cache
Problem statement
A movie cache holds at most capacity distinct movie names. Process the movie names in accesses from left to right.
- Accessing a cached movie makes it most recently used.
- Accessing a new movie inserts it as most recently used.
- If insertion exceeds capacity, evict the least recently used movie.
Return the names remaining in the cache from most recently used to least recently used. If capacity is zero, return an empty array.
Function
recentMovies(capacity: int, accesses: String[]) → String[]Examples
Example 1
capacity = 3accesses = ["Arrival","Heat","Up","Heat","Jaws"]return = ["Jaws","Heat","Up"]Heat is refreshed before Jaws evicts the least-recent Arrival entry.
Example 2
capacity = 2accesses = ["A","B","A"]return = ["A","B"]The final access moves A ahead of B.
Constraints
0 <= capacity <= 100000.0 <= accesses.length <= 200000.- Every movie name is non-empty.