# Question

Formatted question description: https://leetcode.ca/all/146.html

146	LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache.
It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(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.

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.put(4, 4);    // evicts key 1
cache.get(3);       // returns 3
cache.get(4);       // returns 4

Could you do both operations in O(1) time complexity?

@tag-design


# Algorithm

## Solution

Create a class MyNode, which contains data fields int key, int value, Node prev and Node next. That is, each node of type MyNode has a key and a value, and have references to its previous node and the next node.

In class LRUCache, data fields include int capacity that stores the capacity of the cache, Map<Integer, MyNode> map that maps each key to its node, MyNode head and Node tail that represents the head node and the tail node respectively. For Least Recently Used cache, the most recently used node is the head node and the least recently used node is the tail node.

In the constructor, initialize capacity with the given capacity.

In get(key), if key is not in map, then key is not in the cache, so return -1. If key is in map, obtain the node and its value, remove the node and set the node to be the head, and return value.

In put(key, value), if map contains key, then obtain the node and update its value, remove the node, and set the node to be the head. If map does not contain key, then create a new node using key and value, and set the new node to be the head. If the size of map is greater than or equal to capacity, then remove the node tail and remove the corresponding entry in map. Add a new entry of the new node into the map.

Two supplementary methods are needed.

1. Method remove(MyNode node). Obtain MyNode’s previous node and next node, and update their references to other nodes accordingly. If MyNode is head or tail, then update head or tail accordingly.
2. Method setHead(MyNode node). Set MyNode to be the new head and set the previous head’s reference accordingly. If tail is null, then update tail as well.

# Code

Java

class LRUCache {
int capacity;
Map<Integer, MyNode> map = new HashMap<Integer, MyNode>(); // key => Node[key,val]
MyNode tail = null;

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {
MyNode node = map.get(key);
if (node == null) {
return -1;
} else {
remove(node);
return node.value;
}
}

public void put(int key, int value) {
if (map.containsKey(key)) {
MyNode node = map.get(key);
node.value = value;
remove(node);
} else {
MyNode node = new MyNode(key, value);
map.put(key, node);
if (map.size() >= capacity) { // @note: also = , after if will add one more
map.remove(tail.key);
remove(tail);
}
}
}

public void remove(MyNode node) {
MyNode prev = node.prev;
MyNode next = node.next;

// process previous node
if (prev != null)
prev.next = next;
else

// process next node
if (next != null)
next.prev = prev;
else
tail = prev;
}

node.prev = null;
if (tail == null)
}
}

class MyNode {
int key;
int value;
MyNode prev;
MyNode next;

public MyNode(int key, int value) {
this.key = key;
this.value = value;
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/