LeetCode 1756: Design Most Recently Used Queue Solution
Master LeetCode problem 1756 (Design Most Recently Used Queue), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1756. Design Most Recently Used Queue
Problem Explanation
Explanation:
To design a Most Recently Used (MRU) queue, we can use a combination of a doubly linked list and a hashmap. The doubly linked list will store the elements in the order of their most recent usage, and the hashmap will store the mapping of the element value to its corresponding node in the linked list.
The key operations we need to support are:
put(int key, int value): Add or update an element in the queue. If the element already exists, we update its value and move it to the front of the queue.get(int key): Retrieve the value of the element with the given key. If the element exists, we move it to the front of the queue.
By maintaining a doubly linked list and a hashmap, we can achieve constant time complexity for both put and get operations.
:
Solution Code
import java.util.HashMap;
class Node {
int key, value;
Node prev, next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
class MRUQueue {
private final int capacity;
private final HashMap<Integer, Node> map;
private Node head, tail;
public MRUQueue(int capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
this.head = new Node(-1, -1);
this.tail = new Node(-1, -1);
head.next = tail;
tail.prev = head;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
Node node = map.get(key);
node.value = value;
moveToFront(node);
} else {
if (map.size() == capacity) {
evict();
}
Node newNode = new Node(key, value);
map.put(key, newNode);
addToFront(newNode);
}
}
public int get(int key) {
if (!map.containsKey(key)) {
return -1;
}
Node node = map.get(key);
moveToFront(node);
return node.value;
}
private void addToFront(Node node) {
node.next = head.next;
node.prev = head;
head.next.prev = node;
head.next = node;
}
private void moveToFront(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
addToFront(node);
}
private void evict() {
Node toRemove = tail.prev;
map.remove(toRemove.key);
tail.prev = toRemove.prev;
toRemove.prev.next = tail;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1756 (Design Most Recently Used Queue)?
This page provides optimized solutions for LeetCode problem 1756 (Design Most Recently Used Queue) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1756 (Design Most Recently Used Queue)?
The time complexity for LeetCode 1756 (Design Most Recently Used Queue) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1756 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1756 in Java, C++, or Python.