dev-resources.site
for different kinds of informations.
Implement LRU Cache
Published at
12/4/2024
Categories
machine
coding
interview
system
Author
prashantrmishra
Author
15 person written this
prashantrmishra
open
class LRUCache {
int capacity = 0;
Map<Integer,Integer> map;
public LRUCache(int capacity) {
map= new LinkedHashMap<>();
this.capacity = capacity;
}
public int get(int key) {
int val = -1;
if(map.containsKey(key)) {
val = map.get(key);
map.remove(key);
map.put(key,val);
}
return val;
}
public void put(int key, int value) {
if(map.containsKey(key)) map.remove(key);
if(map.size() == this.capacity) map.remove(map.entrySet().iterator().next().getKey());
map.put(key,value);
}
}
/**
* 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);
*/
With using doubly LinkedList
O(1) for read and write
class LRUCache {
DLinkedList head = null;
DLinkedList tail = null;
int capacity = 0;
Map<Integer, DLinkedList> map = new HashMap<>();
public LRUCache(int capacity) {
this.capacity = capacity;
}
public int get(int key) {
DLinkedList node = map.getOrDefault(key, null);
if (node == null)
return -1;
// Move the accessed node to the end (most recently used)
remove(node);
addToTail(node);
return node.value;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
// Key exists; update value and move to the end
DLinkedList node = map.get(key);
node.value = value;
remove(node);
addToTail(node);
} else {
// Key doesn't exist; create a new node
if (map.size() == capacity) {
// Evict the least recently used (head node)
map.remove(head.key);
remove(head);
}
DLinkedList newNode = new DLinkedList(key, value);
addToTail(newNode);
map.put(key, newNode);
}
}
private void remove(DLinkedList node) {
if (node.prev != null) {
node.prev.next = node.next;
} else {
// Node is head
head = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
} else {
// Node is tail
tail = node.prev;
}
node.prev = null;
node.next = null;
}
private void addToTail(DLinkedList node) {
if (tail == null) {
// List is empty
head = tail = node;
} else {
// Add to end of the list
tail.next = node;
node.prev = tail;
tail = node;
}
}
public void traverse() {
DLinkedList n = head;
while (n != null) {
System.out.print(n.prev + "<--[" + n.key + "," + n.value + "]-->" + n.next);
n = n.next;
}
System.out.println();
}
}
class DLinkedList {
DLinkedList prev;
DLinkedList next;
int key;
int value;
public DLinkedList(int k, int v) {
this.key = k;
this.value = v;
}
}
/**
* 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);
*/
system Article's
30 articles in total
You Won’t Believe What Happens When You Click ‘Upload’
read article
Understanding Commodore Operating Systems
read article
Implement LRU Cache
currently reading
Conway’s Law - How Organizational Communication Shapes System Design
read article
kafka deep dive
read article
Please Install It for Me
read article
Reconstructing Shopping Bill Dataset from OCR Data using Bash
read article
5 text processing tools `grep, sed, awk, cut, and tr` to score some marks in System Commands OPPE
read article
10 Linux commands to score 25 marks in System Commands OPPE
read article
AWS Systems Manager Session Manager Hands-on: Securely Manage Your EC2 Instances from Anywhere
read article
LED display principle and system composition
read article
The Power of System Integration Testing: 5 Transformative Benefits
read article
Unveiling the Crucial Importance of System Integration Testing
read article
System Integration Testing Guide: All You Need To Know
read article
6 Features of Invoicing Software That Simplify Your Workday in 2024
read article
Exploring System Integration Testing Techniques: A Comprehensive Guide
read article
A Comprehensive Guide to Multi-Tenancy Architecture
read article
Benefits of the Oracle Warehouse Management System
read article
array, dictionary, for and while loops to score 25 marks in System Commands OPPE
read article
Zeep Service -Your Travel Partner(Low-Level Design)
read article
5 Mistakes to Avoid While Going for System Integration Testing
read article
Reasons to Go for System Integration Testing in Software Testing
read article
Benefits of System Integration Testing Tools in 2024
read article
5 Tips to Choose the Best System Integration Testing Tool
read article
The Invaluable Advantages of System Integration Testing (SIT)
read article
RDBMS: Key Concepts and Principles of Relational Database Management System
read article
Mistakes to Steer Clear of During System Integration Testing
read article
The Vital Importance of System Integration Testing (SIT)
read article
Challenges in System Integration Testing and Opkey’s Solutions
read article
Everything You Should Know About Integrated System Testing
read article
Featured ones: