CódigoSin intentar
Caché LRU en O(1)
Dificultad: media
new LRUCache<K, V>(capacity) con get y put en O(1).
getdevuelve el valor oundefined; un acierto convierte la clave en la usada más recientementeputinserta o actualiza (actualizar también la refresca); si se supera la capacidad → expulsa la usada hace más tiemposizedevuelve el número de entradas
Prepárate para explicar por qué un Map te da esto gratis, y cómo lo construirías sin él (tabla hash + lista doblemente enlazada).
Ver solución de referencia
// A Map remembers insertion order, so its first key is always the least recently used.// On every get or put we delete the key and set it again, which moves it to the end.export class LRUCache<K, V> { private map = new Map<K, V>();
constructor(private capacity: number) {}
get(key: K): V | undefined { if (!this.map.has(key)) { return undefined; } const value = this.map.get(key)!; this.map.delete(key); this.map.set(key, value); // now the most recent return value; }
put(key: K, value: V): void { this.map.delete(key); this.map.set(key, value); if (this.map.size > this.capacity) { const oldestKey = this.map.keys().next().value as K; this.map.delete(oldestKey); } }
get size(): number { return this.map.size; }}
Ctrl Enter