mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
57 lines
2.5 KiB
HTML
57 lines
2.5 KiB
HTML
<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>
|
|
|
|
<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will return a cached value.</p>
|
|
|
|
<p><code>fn</code> can be any function and there are no constraints on what type of values it accepts. Inputs are considered identical if they are <code>===</code> to each other.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong>
|
|
getInputs = () => [[2,2],[2,2],[1,2]]
|
|
fn = function (a, b) { return a + b; }
|
|
<strong>Output:</strong> [{"val":4,"calls":1},{"val":4,"calls":1},{"val":3,"calls":2}]
|
|
<strong>Explanation:</strong>
|
|
const inputs = getInputs();
|
|
const memoized = memoize(fn);
|
|
for (const arr of inputs) {
|
|
memoized(...arr);
|
|
}
|
|
|
|
For the inputs of (2, 2): 2 + 2 = 4, and it required a call to fn().
|
|
For the inputs of (2, 2): 2 + 2 = 4, but those inputs were seen before so no call to fn() was required.
|
|
For the inputs of (1, 2): 1 + 2 = 3, and it required another call to fn() for a total of 2.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong>
|
|
getInputs = () => [[{},{}],[{},{}],[{},{}]]
|
|
fn = function (a, b) { return ({...a, ...b}); }
|
|
<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":2},{"val":{},"calls":3}]
|
|
<strong>Explanation:</strong>
|
|
Merging two empty objects will always result in an empty object. It may seem like there should only be 1 call to fn() because of cache-hits, however none of those objects are === to each other.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong>
|
|
getInputs = () => { const o = {}; return [[o,o],[o,o],[o,o]]; }
|
|
fn = function (a, b) { return ({...a, ...b}); }
|
|
<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":1},{"val":{},"calls":1}]
|
|
<strong>Explanation:</strong>
|
|
Merging two empty objects will always result in an empty object. The 2nd and 3rd third function calls result in a cache-hit. This is because every object passed in is identical.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= inputs.length <= 10<sup>5</sup></code></li>
|
|
<li><code>0 <= inputs.flat().length <= 10<sup>5</sup></code></li>
|
|
<li><code>inputs[i][j] != NaN</code></li>
|
|
</ul>
|