2630. Memoize II LeetCode Solution

In this guide, you will get 2630. Memoize II LeetCode Solution with the best time and space complexity. The solution to Memoize II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Memoize II solution in C++
  4. Memoize II solution in Java
  5. Memoize II solution in Python
  6. Additional Resources
2630. Memoize II LeetCode Solution image

Problem Statement of Memoize II

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.

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}]
Explanation:
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.

Example 2:

Input:
getInputs = () => [[{},{}],[{},{}],[{},{}]]
fn = function (a, b) { return ({…a, …b}); }
Output: [{“val”:{},”calls”:1},{“val”:{},”calls”:2},{“val”:{},”calls”:3}]
Explanation:
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.

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}]
Explanation:
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.

Constraints:

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

Complexity Analysis

  • Time Complexity: Google AdSense
  • Space Complexity: Google Analytics

2630. Memoize II LeetCode Solution in C++

type Fn = (...params: any) => any;

function memoize(fn: Fn): Fn {
  const root = new Map(); // trie
  const ansKey = {};
  return function (...params) {
    let node = root;
    for (const param of params) {
      let next = node.get(param);
      if (next === undefined) {
        next = new Map();
        node.set(param, next);
      }
      node = next;
    }

    // Check if `ansKey` has been set.
    if (node.has(ansKey)) return node.get(ansKey);
    const ans = fn(...params);
    node.set(ansKey, ans);
    return ans;
  };
}
/* code provided by PROGIEZ */

2630. Memoize II LeetCode Solution in Java

N/A
// code provided by PROGIEZ

2630. Memoize II LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.