leetcode 30 days js challenge
Medium
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.
You can assume there are 3 possible input functions: sum
, fib
, and factorial
.
sum
accepts two integers a
and b
and returns a + b
.fib
accepts a single integer n
and returns 1
if n <= 1
or fib(n - 1) + fib(n - 2)
otherwise.factorial
accepts a single integer n
and returns 1
if n <= 1
or factorial(n - 1) * n
otherwise.Example 1:
Example 2:
Example 3:
Constraints:
1 <= n <= 10
at most 105 function calls
at most 105 attempts to access callCount
input function is sum, fib, or factorial
SheepSat, May 13, 2023