Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 845b820

Browse files
30 days javaScript 11th problme solved
1 parent 8b35328 commit 845b820

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
3+
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+
You can assume there are 3 possible input functions: sum, fib, and factorial.
9+
10+
sum accepts two integers a and b and returns a + b. Assume that if a value has already been cached for the arguments (b, a) where a != b, it cannot be used for the arguments (a, b). For example, if the arguments are (3, 2) and (2, 3), two separate calls should be made.
11+
fib accepts a single integer n and returns 1 if n <= 1 or fib(n - 1) + fib(n - 2) otherwise.
12+
factorial accepts a single integer n and returns 1 if n <= 1 or factorial(n - 1) * n otherwise.
13+
14+
15+
Example 1:
16+
17+
Input:
18+
fnName = "sum"
19+
actions = ["call","call","getCallCount","call","getCallCount"]
20+
values = [[2,2],[2,2],[],[1,2],[]]
21+
Output: [4,4,1,3,2]
22+
Explanation:
23+
const sum = (a, b) => a + b;
24+
const memoizedSum = memoize(sum);
25+
memoizedSum(2, 2); // "call" - returns 4. sum() was called as (2, 2) was not seen before.
26+
memoizedSum(2, 2); // "call" - returns 4. However sum() was not called because the same inputs were seen before.
27+
// "getCallCount" - total call count: 1
28+
memoizedSum(1, 2); // "call" - returns 3. sum() was called as (1, 2) was not seen before.
29+
// "getCallCount" - total call count: 2
30+
Example 2:
31+
32+
Input:
33+
fnName = "factorial"
34+
actions = ["call","call","call","getCallCount","call","getCallCount"]
35+
values = [[2],[3],[2],[],[3],[]]
36+
Output: [2,6,2,2,6,2]
37+
Explanation:
38+
const factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));
39+
const memoFactorial = memoize(factorial);
40+
memoFactorial(2); // "call" - returns 2.
41+
memoFactorial(3); // "call" - returns 6.
42+
memoFactorial(2); // "call" - returns 2. However factorial was not called because 2 was seen before.
43+
// "getCallCount" - total call count: 2
44+
memoFactorial(3); // "call" - returns 6. However factorial was not called because 3 was seen before.
45+
// "getCallCount" - total call count: 2
46+
Example 3:
47+
48+
Input:
49+
fnName = "fib"
50+
actions = ["call","getCallCount"]
51+
values = [[5],[]]
52+
Output: [8,1]
53+
Explanation:
54+
fib(5) = 8 // "call"
55+
// "getCallCount" - total call count: 1
56+
57+
58+
Constraints:
59+
60+
0 <= a, b <= 105
61+
1 <= n <= 10
62+
1 <= actions.length <= 105
63+
actions.length === values.length
64+
actions[i] is one of "call" and "getCallCount"
65+
fnName is one of "sum", "factorial" and "fib"
66+
67+
68+
69+
70+
*/
71+
72+
73+
74+
75+
type Fn = (...params: number[]) => number;
76+
77+
function memoize(fn: Fn): Fn {
78+
const cache: { [key: string]: number } = {};
79+
80+
return function (...args: number[]): number {
81+
const key = args.join(',');
82+
if (key in cache) {
83+
return cache[key];
84+
}
85+
const result = fn(...args);
86+
cache[key] = result;
87+
return result;
88+
}
89+
}
90+
91+
92+
/**
93+
* let callCount = 0;
94+
* const memoizedFn = memoize(function (a, b) {
95+
* callCount += 1;
96+
* return a + b;
97+
* })
98+
* memoizedFn(2, 3) // 5
99+
* memoizedFn(2, 3) // 5
100+
* console.log(callCount) // 1
101+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /