import java.util.HashMap
import java.util.Map
class Node {
int k, v
Node l, r
Node(int _k, int _v) {
k = _k
v = _v
}
}
public class LRUcache {
int n
Node head, tail
Map<Integer, Node> map
public LRUcache(int capacity) {
n = capacity
map = new HashMap<>()
head = new Node(-1, -1)
tail = new Node(-1, -1)
head.r = tail
tail.l = head
}
public int get(int key) {
if (map.containsKey(key)) {
Node node = map.get(key)
refresh(node)
return node.v
}
return -1
}
public void put(int key, int value) {
Node node = null
if (map.containsKey(key)) {
node = map.get(key)
node.v = value
} else {
if (map.size() == n) {
Node del = tail.l
map.remove(del.k)
delete(del)
}
node = new Node(key, value)
map.put(key, node)
}
refresh(node)
}
void refresh(Node node) {
delete(node)
node.r = head.r
node.l = head
head.r.l = node
head.r = node
}
void delete(Node node) {
if (node.l != null) {
Node left = node.l
left.r = node.r
node.r.l = left
}
}
public static void main(String[] args) {
LRUcache lrUcache = new LRUcache(5)
lrUcache.put(12, 34)
System.out.println(lrUcache.get(12))
System.out.println(lrUcache.head.k)
}
}