class LRUCache {↵
constructor(capacity) {↵
this.capacity = capacity;↵
this.cache = new Map();↵
}↵
get(key) {↵
if (!this.cache.has(key)) return -1;↵
const val = this.cache.get(key);↵
this.cache.delete(key);↵
this.cache.set(key, val);↵
return val;↵
}↵
put(key, value) {↵
this.cache.delete(key);↵
this.cache.set(key, value);↵
if (this.cache.size > this.capacity) {↵
this.cache.delete(this.cache.keys().next().value);↵
}↵
}↵
}↵
↵
const cache = new LRUCache(2);↵
cache.put(1, "a"); cache.put(2, "b");↵
console.log(cache.get(1));↵
cache.put(3, "c");↵
console.log(cache.get(2));