-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-146-LRU-Cache.java
150 lines (126 loc) · 3.86 KB
/
LeetCode-146-LRU-Cache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
LeetCode: https://leetcode.com/problems/lru-cache/
LintCode: http://www.lintcode.com/problem/lru-cache/
JiuZhang: http://www.jiuzhang.com/solutions/lru-cache/
ProgramCreek: http://www.programcreek.com/2013/03/leetcode-lru-cache-java/
Analysis:
Two key points to design a LRU cache:
1.Get,Set,Remove in O(1) (Use HashMap)
2.Maintain the order from latest and oldest (Use Double Linked List)
Use HashMap to search,delete,set: O(1)
List to keep order(like a queue, FILO)
Be carefully, every get, set operation, will cause operated node to the head of list.
Every set operation will affect both List and HashMap.
This Question could be extended to add some other API:
clear(); // clear the cache
delete(int key); // delete one key-value in the cache
size(); // return size of the cache
*/
/*
tail <- prev <- Node -> next -> head
*/
// 1. Using Node class
class LRUCache {
private class Node {
int key;
int value;
Node prev;
Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
Map<Integer, Node> map;
Node tail;
Node head;
int capacity;
public LRUCache(int capacity) {
this.capacity = capacity;
map = new HashMap<>(capacity);
}
public int get(int key) {
if (map.keySet().contains(key)) {
Node curr = map.get(key);
removeNode(curr);
addToHead(curr);
return curr.value;
}
return -1;
}
public void put(int key, int value) {
// simple case, if already exists, just update the value, and promote the node to head
if (map.keySet().contains(key)) {
Node curr = map.get(key);
curr.value = value;
removeNode(curr);
addToHead(curr);
return;
}
// going to add a new node, but check the capacity first
if (map.size() == capacity) {
// remove tail first
map.remove(tail.key);
removeNode(tail);
}
// first time to add this key-value pair
Node curr = new Node(key, value);
map.put(key, curr);
addToHead(curr);
}
// Operation to Double LinkedList (has nothing to do with HashMap)
private void removeNode(Node node) {
if (node == tail) tail = tail.next;
if (node == head) head = head.prev;
if (node.next != null) node.next.prev = node.prev;
if (node.prev != null) node.prev.next = node.next;
node.next = null;
node.prev = null;
}
// Operation to Double LInkedList (has nothing to do with HashMap)
private void addToHead(Node node) {
if (head == null) {
head = node;
tail = node;
return;
}
head.next = node;
node.prev = head;
head = head.next;
}
}
// 2. Using LinkedHashMap
class LRUCache {
Map<Integer, Integer> map;
int capacity;
public LRUCache(int capacity) {
this.capacity = capacity;
this.map = new LinkedHashMap<>(capacity);
}
public int get(int key) {
if (map.containsKey(key)) {
int value = map.remove(key);
map.put(key, value);
return value;
}
return -1;
}
public void put(int key, int value) {
if(map.containsKey(key)) {
map.remove(key);
map.put(key, value);
return;
}
map.put(key, value);
if(map.size() > capacity) {
int eldestKey = map.keySet().iterator().next();
map.remove(eldestKey);
}
}
}
/**
* 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);
*/