Skip to content

Commit 66972ce

Browse files
Update README.md
1 parent c440ce5 commit 66972ce

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

L-A/0014 Memoize II/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1+
# Memoize II
2+
[LeetCode Problem]https://leetcode.com/problems/memoize-ii/
13

4+
Given a function fn, return a memoized version of that function.
5+
6+
A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.
7+
8+
fn can be any function and there are no constraints on what type of values it accepts. Inputs are considered identical if they are === to each other.
9+
10+
## Example 1:
11+
```javascript
12+
Input:
13+
getInputs = () => [[2,2],[2,2],[1,2]]
14+
fn = function (a, b) { return a + b; }
15+
Output: [{"val":4,"calls":1},{"val":4,"calls":1},{"val":3,"calls":2}]
16+
Explanation:
17+
const inputs = getInputs();
18+
const memoized = memoize(fn);
19+
for (const arr of inputs) {
20+
memoized(...arr);
21+
}
22+
23+
For the inputs of (2, 2): 2 + 2 = 4, and it required a call to fn().
24+
For the inputs of (2, 2): 2 + 2 = 4, but those inputs were seen before so no call to fn() was required.
25+
For the inputs of (1, 2): 1 + 2 = 3, and it required another call to fn() for a total of 2.
26+
```

0 commit comments

Comments
 (0)