1756. Design Most Recently Used Queue
https://leetcode.com/problems/design-most-recently-used-queue
Description
Design a queue-like data structure that moves the most recently used element to the end of the queue.
Implement the MRUQueue
class:
MRUQueue(int n)
constructs theMRUQueue
withn
elements:[1,2,3,...,n]
.int fetch(int k)
moves thekth
element (1-indexed) to the end of the queue and returns it.
Example 1:
Constraints:
1 <= n <= 2000
1 <= k <= n
At most
2000
calls will be made tofetch
.
Follow up: Finding an O(n)
algorithm per fetch
is a bit easy. Can you find an algorithm with a better complexity for each fetch
call?
ac
Last updated