Skip to content

Commit 145625b

Browse files
day 11 task complete
1 parent 180a21e commit 145625b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

day11/leetcode2623.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// 2623. Memoize
2+
// URL -> https://leetcode.com/problems/memoize/
3+
4+
/**
5+
* @param {Function} fn
6+
* @return {Function}
7+
*/
8+
function memoize(fn) {
9+
let cache = {}
10+
return function (...args) {
11+
const key = args.join(",")
12+
if ((key in cache)) {
13+
return cache[key];
14+
}
15+
const calc = fn(...args);
16+
cache[key] = calc;
17+
return calc;
18+
19+
}
20+
}
21+
22+
23+
/**
24+
* let callCount = 0;
25+
* const memoizedFn = memoize(function (a, b) {
26+
* callCount += 1;
27+
* return a + b;
28+
* })
29+
* memoizedFn(2, 3) // 5
30+
* memoizedFn(2, 3) // 5
31+
* console.log(callCount) // 1
32+
*/

0 commit comments

Comments
 (0)