LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Follow up: Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache( 2 / capacity / );
cache.put(1, 1); cache.put(2, 2); cache.get(1); // returns 1 cache.put(3, 3); // evicts key 2 cache.get(2); // returns -1 (not found) cache.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not found) cache.get(3); // returns 3 cache.get(4); // returns 4
Solution
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Gong Li <[email protected]> on 28/6/2016.
*/
public class LRUCache {
private class Node {
Node prev;
Node next;
int key;
int value;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
Map<Integer, Node> map;
Node head;
Node tail;
int capacity;
public LRUCache(int capacity) {
map = new HashMap<>();
head = new Node(-1, -1);
tail = new Node(-1, -1);
head.next = tail;
tail.prev = head;
this.capacity = capacity;
}
public int get(int key) {
if (!map.containsKey(key)) {
return -1;
}
Node currentNode = map.get(key);
Node prev = currentNode.prev;
Node next = currentNode.next;
prev.next = next;
next.prev = prev;
moveToTail(currentNode);
return map.get(key).value;
}
public void set(int key, int value) {
if (get(key) != -1) {
map.get(key).value = value;
} else {
if (map.size() == capacity) {
// remove the first Node
Node currentNode = head.next;
Node prev = currentNode.prev;
Node next = currentNode.next;
prev.next = next;
next.prev = prev;
map.remove(currentNode.key);
}
// add the new node to the tail
Node newNode = new Node(key, value);
moveToTail(newNode);
map.put(key, newNode);
}
}
private void moveToTail(Node node) {
Node prev = tail.prev;
prev.next = node;
node.prev = prev;
node.next = tail;
tail.prev = node;
}
/**
*
* 2,[set(2,1),set(1,1),set(2,3),set(4,1),get(1),get(2)]
Output:
[1,-1]
Expected:
[-1,3]
*
*/
}