-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://leetcode.com/problems/lfu-cache/
- Loading branch information
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
interview_prep/algorithm/java/ide_handicapped/lfu_cache/fastest/LFUCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
class LFUCache { | ||
class Node { | ||
int key; | ||
int val; | ||
int cnt = 1; | ||
Node prev; | ||
Node next; | ||
Node(int key, int val) { | ||
this.key = key; | ||
this.val = val; | ||
} | ||
} | ||
Node[] keys = new Node[100001]; | ||
Node[] cnts = new Node[200001]; | ||
Node tail; | ||
int size = 0; | ||
int capacity; | ||
|
||
public LFUCache(int capacity) { | ||
this.capacity = capacity; | ||
} | ||
|
||
void remove(Node node) { | ||
Node prev = node.prev; | ||
Node next = node.next; | ||
|
||
if (prev != null) | ||
prev.next = next; | ||
|
||
if (next != null) | ||
next.prev = prev; | ||
|
||
keys[node.key] = null; | ||
if (cnts[node.cnt] == node) { | ||
if (next != null && next.cnt == node.cnt) | ||
cnts[node.cnt] = next; | ||
else | ||
cnts[node.cnt] = null; | ||
} | ||
|
||
if (tail == node) | ||
tail = prev; | ||
|
||
node.prev = null; | ||
node.next = null; | ||
} | ||
|
||
void insert(Node node, Node next) { | ||
cnts[node.cnt] = node; | ||
keys[node.key] = node; | ||
if (next == null) { | ||
if (tail != null) | ||
tail.next = node; | ||
node.prev = tail; | ||
tail = node; | ||
} | ||
else { | ||
if (next.prev != null) | ||
next.prev.next = node; | ||
node.prev = next.prev; | ||
node.next = next; | ||
next.prev = node; | ||
} | ||
} | ||
|
||
void moveUp(Node node) { | ||
Node next = cnts[node.cnt + 1] != null ? cnts[node.cnt + 1] : cnts[node.cnt]; | ||
if (next == node) | ||
next = node.next; | ||
|
||
remove(node); | ||
node.cnt++; | ||
insert(node, next); | ||
} | ||
|
||
public int get(int key) { | ||
Node node = keys[key]; | ||
|
||
if (node == null) | ||
return -1; | ||
|
||
moveUp(node); | ||
|
||
return node.val; | ||
} | ||
|
||
public void put(int key, int value) { | ||
Node node = keys[key]; | ||
if (node == null) { | ||
node = new Node(key, value); | ||
if (size >= capacity) | ||
remove(tail); | ||
else | ||
size++; | ||
|
||
insert(node, cnts[1]); | ||
} | ||
else { | ||
moveUp(node); | ||
node.val = value; | ||
} | ||
} | ||
} |