{ capacity: number, operations: [{type:"get"|"put", key, value?}] }
1class LRUCache(capacity):2 map = HashMap3 list = DoublyLinkedList45 function get(key):6 if key not in map: return -17 move node to front of list8 return node.value910 function put(key, value):11 if key in map:12 update value, move to front13 else:14 if size >= capacity:15 evict tail node16 insert new node at front17 map[key] = new node
1class LRUCache {2 private cap: number;3 private cache = new Map<number, { val: number }>();45 constructor(capacity: number) {6 this.cap = capacity;7 }89 get(key: number): number {10 if (!this.cache.has(key)) return -1;11 const val = this.cache.get(key)!.val;12 this.cache.delete(key);13 this.cache.set(key, { val });14 return val;15 }1617 put(key: number, value: number): void {18 this.cache.delete(key);19 if (this.cache.size >= this.cap) {20 const oldest = this.cache.keys().next().value!;21 this.cache.delete(oldest);22 }23 this.cache.set(key, { val: value });24 }25}