Solución
solution.tsTypeScript
export function lruCache(capacity: number, ops: Array<["get" | "put", number, number?]>): (number | null)[] {
const resp = []
let cache: number[][] = []
ops.forEach(op => {
switch (op[0]) {
case 'get': {
let index = -1
for (let i = 0; i < cache.length; i++) {
if (cache[i][0] === op[1]) {
index = i
break
}
}
if (index === -1) {
resp.push(-1)
} else {
const [key, value] = cache[index]
cache.splice(index, 1)
cache.push([key, value])
resp.push(value)
}
break
}
case 'put': {
let index = -1
for (let i = 0; i < cache.length; i++) {
if (cache[i][0] === op[1]) {
index = i
break
}
}
if(index !== -1){
cache.splice(index,1)
}
if(cache.length === capacity){
cache.shift()
}
cache.push([op[1], op[2]])
resp.push(null)
break
}
}
})
return resp
}0respuestas