1class LRUCache {
2 constructor(capacity) { this.cap = capacity; this.map = new Map(); }
3 get(key) {
4 if (!this.map.has(key)) return -1;
5 const val = this.map.get(key);
6 this.map.delete(key); this.map.set(key, val);
7 return val;
8 }
9 put(key, val) {
10 this.map.delete(key);
11 if (this.map.size >= this.cap) this.map.delete(this.map.keys().next().value);
12 this.map.set(key, val);
13 }
14}