Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions.
The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).
The function composition of an empty list of functions is the identity function f(x) = x.
You may assume each function in the array accepts one integer as input and returns one integer as output.
Example 1:
Input: functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
Output: 65
Explanation:
Evaluating from right to left ...
Starting with x = 4.
2 * (4) = 8
(8) * (8) = 64
(64) + 1 = 65
Example 2:
Input: functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
Output: 1000
Explanation:
Evaluating from right to left ...
10 * (1) = 10
10 * (10) = 100
10 * (100) = 1000
Example 3:
Input: functions = [], x = 42
Output: 42
Explanation:
The composition of zero functions is the identity function
출처 : https://leetcode.com/problems/function-composition/description/
type F = (x: number) => number;
function compose(functions: F[]): F {
return function (x) {
let result = x;
functions.reverse().forEach(fn => {
result = fn(result)
})
return result
};
}
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/
'LeetCode' 카테고리의 다른 글
| 1672. Richest Customer Wealth / TypeScript (1) | 2024.06.02 |
|---|---|
| 589. N-ary Tree Preorder Traversal / TypeScript (1) | 2024.06.01 |
| 2566. Maximum Difference by Remapping a Digit / TypeScript (0) | 2024.05.30 |
| 2293. Min Max Game / TypeScript (0) | 2024.05.29 |
| 938. Range Sum of BST / TypeScript (0) | 2024.05.28 |