Flatten Deeply Nested Array Problem
Description
LeetCode Problem 2625.
Given a multi-dimensional array arr and a depth n, return a flattened version of that array.
A multi-dimensional array is a recursive data structure that contains integers or other multi-dimensional arrays.
A flattened array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is less than n. The depth of the elements in the first array are considered to be 0.
Please solve it without the built-in Array.flat method.
Example 1:
Input
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
n = 0
Output
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
Explanation
Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened.
Example 2:
Input
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
n = 1
Output
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
Explanation
The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.
Example 3:
Input
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
n = 2
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Explanation
The maximum depth of any subarray is 1. Thus, all of them are flattened.
Constraints:
0 <= count of numbers in arr <= 10^50 <= count of subarrays in arr <= 10^5maxDepth <= 1000-1000 <= each number <= 10000 <= n <= 1000
Solution
Approach: Recursion with a depth budget
Treat n as the number of levels we are still allowed to open. If n is 0, return the array as it is. Otherwise build a new array: a plain number is appended; a sub-array is flattened recursively with budget n - 1 and its elements are appended.
At the top level the elements have depth 0, so with n = 1 the sub-arrays directly in arr get opened, but arrays inside them (depth 1) stay intact. That matches the budget going down by one per level. The recursion is at most maxDepth (1000) frames deep, which is fine.
Complexity: O(m) time over all the elements m visited (each is copied once per level it is flattened through, at most n times); O(m) extra space.
/**
* @param {Array} arr
* @param {number} depth
* @return {Array}
*/
var flat = function (arr, n) {
if (n === 0) {
return arr;
}
const result = [];
for (const item of arr) {
if (Array.isArray(item)) {
for (const x of flat(item, n - 1)) {
result.push(x);
}
} else {
result.push(item);
}
}
return result;
};