Skip to content

Practice Memoize II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2630
LeetCode Problem

Memoize II

Problem Statement

Given a function fn, return a memoized version of that function. A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value. 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.

Source: Memoize II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
getInputs = () => [[2,2],[2,2],[1,2]] fn = function (a, b) { return a + b; }
Output
[{"val":4,"calls":1},{"val":4,"calls":1},{"val":3,"calls":2}]

# Example 2

Input
getInputs = () => [[{},{}],[{},{}],[{},{}]] fn = function (a, b) { return ({...a, ...b}); }
Output
[{"val":{},"calls":1},{"val":{},"calls":2},{"val":{},"calls":3}]

# Example 3

Input
getInputs = () => { const o = {}; return [[o,o],[o,o],[o,o]]; } fn = function (a, b) { return ({...a, ...b}); }
Output
[{"val":{},"calls":1},{"val":{},"calls":1},{"val":{},"calls":1}]

Constraints

1 <= inputs.length <= 10^5
0 <= inputs.flat().length <= 10^5
inputs[i][j] != NaN

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.