Skip to content

Commit 9c4db3d

Browse files
refactor 146
1 parent 3c20ac8 commit 9c4db3d

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ Your ideas/fixes/algorithms are more than welcome!
470470
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_149.java)| O(?)|O(?) | Hard|
471471
|148|[Sort List](https://leetcode.com/problems/sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_148.java) O(nlogn)|O(h) | Medium| Linked List, Sort
472472
|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_147.java) O(n^2)|O(1) | Medium| Linked List
473-
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../master/leetcode-algorithms/src/main/java/com/fishercoder/solutions/_146.java)| amortized O(1)| O(n) | Hard| Linked List
473+
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../master/leetcode-algorithms/src/main/java/com/fishercoder/solutions/_146.java)| amortized O(1)| O(k) | Hard| Doubly Linked List, LinkedHashMap
474474
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_145.java)| O(n)|O(h) | Hard| Binary Tree
475475
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_144.java)| O(n)|O(h) | Medium| Binary Tree
476476
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_143.java)| O(n)|O(1) | Medium|

src/main/java/com/fishercoder/solutions/_146.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Could you do both operations in O(1) time complexity?
3333

3434
public class _146 {
3535

36-
public class LinkedHashMapSolution {
36+
public class Solution1 {
3737
/**
3838
* The shortest implementation is to use LinkedHashMap:
3939
* specify a size of the linkedHashMap;
@@ -47,7 +47,7 @@ public class LinkedHashMapSolution {
4747
private Map<Integer, Integer> cache;
4848
private final int max;
4949

50-
public LinkedHashMapSolution(int capacity) {
50+
public Solution1(int capacity) {
5151
max = capacity;
5252
cache = new LinkedHashMap<Integer, Integer>(capacity, 1.0f, true) {
5353
public boolean removeEldestEntry(Map.Entry eldest) {
@@ -65,13 +65,14 @@ public void set(int key, int value) {
6565
}
6666
}
6767

68-
public class DoublyLinkedListPlusHashMapSolution {
68+
public class Solution2 {
69+
/**The more verbose solution is to write a doubly linked list plus a map.*/
6970
private class Node {
7071
int key;
7172
int value;
7273

73-
DoublyLinkedListPlusHashMapSolution.Node prev;
74-
DoublyLinkedListPlusHashMapSolution.Node next;
74+
Solution2.Node prev;
75+
Solution2.Node next;
7576

7677
Node(int k, int v) {
7778
this.key = k;
@@ -86,24 +87,24 @@ private class Node {
8687

8788
private int capacity;
8889
private int count;
89-
private DoublyLinkedListPlusHashMapSolution.Node head;
90-
private DoublyLinkedListPlusHashMapSolution.Node tail;
91-
private Map<Integer, DoublyLinkedListPlusHashMapSolution.Node> map;
90+
private Solution2.Node head;
91+
private Solution2.Node tail;
92+
private Map<Integer, Solution2.Node> map;
9293
// ATTN: the value should be Node type! This is the whole point of having a class called Node!
9394

94-
public DoublyLinkedListPlusHashMapSolution(int capacity) {
95+
public Solution2(int capacity) {
9596
this.capacity = capacity;
9697
this.count = 0;// we need a count to keep track of the number of elements in the cache so
9798
// that we know when to evict the LRU one from the cache
9899
this.map = new HashMap();
99-
head = new DoublyLinkedListPlusHashMapSolution.Node();
100-
tail = new DoublyLinkedListPlusHashMapSolution.Node();
100+
head = new Solution2.Node();
101+
tail = new Solution2.Node();
101102
head.next = tail;
102103
tail.prev = head;
103104
}
104105

105106
public int get(int key) {
106-
DoublyLinkedListPlusHashMapSolution.Node node = map.get(key);
107+
Solution2.Node node = map.get(key);
107108
// HashMap allows value to be null, this is superior than HashTable!
108109
if (node == null) {
109110
return -1;
@@ -122,9 +123,9 @@ public int get(int key) {
122123
}
123124

124125
public void set(int key, int value) {
125-
DoublyLinkedListPlusHashMapSolution.Node node = map.get(key);
126+
Solution2.Node node = map.get(key);
126127
if (node == null) {
127-
node = new DoublyLinkedListPlusHashMapSolution.Node(key, value);
128+
node = new Solution2.Node(key, value);
128129
map.put(key, node);
129130
add(node);
130131
count++;
@@ -133,7 +134,7 @@ public void set(int key, int value) {
133134
/** ATTN: It's tail.prev, not tail, because tail is always an invalid node, it
134135
doesn't contain anything, it's always the tail.prev that is the last node in the
135136
cache*/
136-
DoublyLinkedListPlusHashMapSolution.Node toDelete = tail.prev;
137+
Solution2.Node toDelete = tail.prev;
137138
map.remove(toDelete.key);
138139
remove(toDelete);
139140
count--;
@@ -145,16 +146,16 @@ public void set(int key, int value) {
145146
}
146147
}
147148

148-
private void remove(DoublyLinkedListPlusHashMapSolution.Node node) {
149-
DoublyLinkedListPlusHashMapSolution.Node next = node.next;
150-
DoublyLinkedListPlusHashMapSolution.Node prev = node.prev;
149+
private void remove(Solution2.Node node) {
150+
Solution2.Node next = node.next;
151+
Solution2.Node prev = node.prev;
151152
prev.next = next;
152153
next.prev = prev;
153154
}
154155

155-
private void add(DoublyLinkedListPlusHashMapSolution.Node node) {
156+
private void add(Solution2.Node node) {
156157
// ATTN: we'll always add the node into the first position: head.next!!!!
157-
DoublyLinkedListPlusHashMapSolution.Node next = head.next;
158+
Solution2.Node next = head.next;
158159
head.next = node;
159160
node.next = next;
160161
node.prev = head;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy