Implement a LRU cache to keep track of user browsing history. For example a user is browsing your website. The user clicks the pages in the following sequence: page1, page3, page1, page2
According to LRU algorithm, the cache state is [page3, page1, page2].
Explanation:
The user clicks page1, LRU cache state is [page1].
The user clicks page3, LRU cache state is [page1, page3]. We put page3 on top of page1.
The user clicks page1 again, we move the page1 to the top. Now the LRU state is [page3, page1]
Finally the user clicks a new page2, the LRU cache state now is [page3, page1, page2].
Now give a list of strings representing the page links the user has visited, rethrn the the cache state as a list of page links.
Example:
input:
["a", "b", "c", "a"]
output:
["b", "c", "a"]
Copyright © A++ Code Bootcamp 2023