[演算法] Dynamic Programming - Cache and Memoization
Memoization and Cache
基本的使用方式:
type D = {
[index: number]: any;
};
function memoizedFunc() {
let cache: D = {};
return (n: number) => {
if (n in cache) {
return cache[n];
} else {
console.log('do long time calculation...');
cache[n] = n;
return cache[n];
}
};
}
const memoized = memoizedFunc();