Hard
Question
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.
Analysis
HashMap + Doubly LinkedList
LRU,也就是least recently used,最近使用最少的;这样一个数据结构,能够保持一定的顺序,使得最近使用过的时间或者顺序被记录,实际上,具体每一个item最近一次何时被使用的,并不重要,重要的是在这样的一个结构中,item的相对位置代表了最近使用的顺序;满足这样考虑的结构可以是链表list或者数组array,不过前者更有利于insert和delete的操纵,此外,需要记录这个链表的head和tail,方便进行移动到tail或者删除head的操作,即:head.next作为最近最少使用的item,tail.prev为最近使用过的item,在set时,如果超出capacity,则删除head.next,同时将要插入的item放入tail.prev, 而get时,如果存在,只需把item更新到tail.prev即可。
这样set与get均为O(1)
时间的操作 (HashMap Get/Set + LinkedList Insert/Delete),空间复杂度为O(n)
, n为capacity。
LInkedHashMap
使用LinkedHashMap能很方便地实现LRU,定义好access-order,自定义removeEldestEntry()。
Solution
*(Preferred Implementation) Use Doubly Linked List
Copy 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;
this . prev = null ;
this . next = null ;
}
}
private int capacity;
private HashMap < Integer , Node > hm = new HashMap < Integer , Node >();
private Node head = new Node( - 1 , - 1 ) ;
private Node tail = new Node( - 1 , - 1 ) ;
// @param capacity, an integer
public LRUCache ( int capacity) {
this . capacity = capacity;
this . head . next = this . tail ;
this . tail . prev = this . head ;
}
// @return an integer
public int get ( int key) {
if ( ! hm . containsKey (key)) {
return - 1 ;
}
Node current = hm . get (key);
current . prev . next = current . next ;
current . next . prev = current . prev ;
moveToTail(current) ;
return hm . get (key) . value ;
}
// @param key, an integer
// @param value, an integer
// @return nothing
public void set ( int key , int value) {
if ( get(key) != - 1 ) {
hm . get (key) . value = value;
return ;
}
if ( hm . size () == capacity) {
hm . remove ( head . next . key );
head . next = head . next . next ;
head . next . prev = head;
}
Node insert = new Node(key , value) ;
hm . put (key , insert);
moveToTail(insert) ;
}
private void moveToTail ( Node current) {
current . next = tail;
tail . prev . next = current;
current . prev = tail . prev ;
tail . prev = current;
}
}
Another doubly linked list
Copy class Node {
int key;
int value;
Node pre;
Node next;
public Node ( int key , int value){
this . key = key;
this . value = value;
}
}
public class LRUCache {
int capacity;
HashMap < Integer , Node > map = new HashMap < Integer , Node >();
Node head = null ;
Node end = null ;
public LRUCache ( int capacity) {
this . capacity = capacity;
}
public int get ( int key) {
if ( map . containsKey (key)){
Node n = map . get (key);
remove(n) ;
setHead(n) ;
return n . value ;
}
return - 1 ;
}
public void remove ( Node n){
if ( n . pre != null ){
n . pre . next = n . next ;
} else {
head = n . next ;
}
if ( n . next != null ){
n . next . pre = n . pre ;
} else {
end = n . pre ;
}
}
public void setHead ( Node n){
n . next = head;
n . pre = null ;
if (head != null )
head . pre = n;
head = n;
if (end == null )
end = head;
}
public void set ( int key , int value) {
if ( map . containsKey (key)){
Node old = map . get (key);
old . value = value;
remove(old) ;
setHead(old) ;
} else {
Node created = new Node(key , value) ;
if ( map . size () >= capacity){
map . remove ( end . key );
remove(end) ;
setHead(created) ;
} else {
setHead(created) ;
}
map . put (key , created);
}
}
}
Copy public class LRUCache {
//“潜水”链表节点,抽象
static class Node {
//键值对
private int key;
private int value;
//维护“潜水”键值对,双向链表
private Node pre;
private Node next;
//构造器
Node (){}
Node ( int key , int value){
this . key = key;
this . value = value;
}
}
//指定的容量
private int cap;
//保留“潜水”双向链表的头尾指针
private Node head;
private Node tail;
//保存键值对的map
private HashMap < Integer , Node > map;
//构造器参数是:指定的容量
public LRUCache ( int capacity) {
this . cap = capacity;
//初始化头尾节点,这里的头结点是辅助节点
//head节点不存储任何有效元素
head = new Node() ;
tail = head;
//构造器初试容量这样设置可以保证map
//不会发生扩容,详见之前的HashMap
//讲解文章
map = new HashMap <>(( int )(cap / 0.75 ) + 1 );
}
//将指定节点从链表中删除
private void removeNode ( Node cur){
if (cur == tail){
tail = tail . pre ;
tail . next = null ;
cur . pre = null ;
} else {
cur . pre . next = cur . next ;
cur . next . pre = cur . pre ;
cur . pre = null ;
cur . next = null ;
}
}
//将指定节点追加到链表末尾
private void add ( Node cur){
tail . next = cur;
cur . pre = tail;
tail = cur;
}
//访问一个键值对
public int get ( int key) {
Node cur = map . get (key);
//不存在这个key
if (cur == null ){
return - 1 ;
} else { //存在
//含义是当前潜水节点已经被访问了
//将这个节点添加到链表末尾
removeNode(cur) ;
add(cur) ;
return cur . value ;
}
}
//存储一个键值对
public void put ( int key , int value) {
Node cur = map . get (key);
if (cur == null ){
//put前不存在这个key
cur = new Node(key , value) ;
//将该键值对移动到链表末尾
map . put (key , cur);
add(cur) ;
//超出了容量,移除链表头结点
//后面那个元素(头结点是辅助节点)
if ( map . size () > cap && head != tail){
Node outDate = head . next ;
removeNode(outDate) ;
//不能忘记这里
map . remove ( outDate . key );
}
} else {
//put之前已经存在
//将这个键值对移到链表末尾即可
removeNode(cur) ;
add(cur) ;
//更新这个key的值
cur . value = value;
}
}
}
Using LinkedHashMap
[ @StefanPochmann]([https://leetcode.com/problems/lru-cache/discuss/46055/Probably-the-"best"-Java-solution-extend-LinkedHashMap/45473](https://leetcode.com/problems/lru-cache/discuss/46055/Probably-the-"best"-Java-solution-extend-LinkedHashMap/45473)\ )
Copy import java . util . LinkedHashMap ;
public class LRUCache {
private Map < Integer , Integer > map;
private final int maxEntries;
public LRUCache ( int capacity) {
this . maxEntries = capacity;
map = new LinkedHashMap < Integer , Integer >( 16 , 0.75f , true ) {
protected boolean removeEldestEntry ( Map . Entry eldest) {
return size() > capacity;
}
};
}
public int get ( int key) {
return map . getOrDefault (key , - 1 );
}
public void set ( int key , int value) {
map . put (key , value);
}
}
Another by @[sky-xu]([https://leetcode.com/problems/lru-cache/discuss/45939/Laziest-implementation%3A-Java's-LinkedHashMap-takes-care-of-everything](https://leetcode.com/problems/lru-cache/discuss/45939/Laziest-implementation%3A-Java's-LinkedHashMap-takes-care-of-everything)\ )
Copy import java . util . LinkedHashMap ;
public class LRUCache {
private LinkedHashMap < Integer , Integer > map;
private final int CAPACITY;
public LRUCache ( int capacity) {
CAPACITY = capacity;
map = new LinkedHashMap < Integer , Integer >(capacity , 0.75f , true ){
protected boolean removeEldestEntry ( Map . Entry eldest) {
return size() > CAPACITY;
}
};
}
public int get ( int key) {
return map . getOrDefault (key , - 1 );
}
public void set ( int key , int value) {
map . put (key , value);
}
}
Note for LinkedHashMap:
Copy "true for access-order, false for insertion-order"
Follow-up for LRU Cache
Concurrent LRU cache implementation
See stackoverflow:
https://stackoverflow.com/questions/40239485/concurrent-lru-cache-implementation
If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the
Collections.synchronizedMap
method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
The best you can do is to make it thread-safe is to wrap it with Collections.synchronizedMap(map)
as explained in the JavaDoc :
Copy Map m = Collections . synchronizedMap ( new LinkedHashMap( ... ) );
However it is not enough to make it fully thread-safe you sill need to protect any iteration over the content of the map using the instance of the wrapped map as object's monitor:
Copy Map m = Collections . synchronizedMap (map);
...
Set s = m . keySet (); // Needn't be in synchronized block
...
synchronized (m) { // Synchronizing on m, not s!
Iterator i = s . iterator (); // Must be in synchronized block
while ( i . hasNext ())
foo( i . next()) ;
}
Reference
[https://leetcode.com/problems/lru-cache/discuss/45939/Laziest-implementation%3A-Java's-LinkedHashMap-takes-care-of-everything](https://leetcode.com/problems/lru-cache/discuss/45939/Laziest-implementation%3A-Java's-LinkedHashMap-takes-care-of-everything )
https://www.programcreek.com/2013/03/leetcode-lru-cache-java/
https://www.jianshu.com/p/ee6343126728