Optimized Recursive Array Flattening -- Best Solution!! Well documented!!
\n# Runtime Performance\n- Runtime: 89 ms  \n- Beats: 91.32% of submissions\n\nThis high performance is due to not using array spread operations. The only issue
\n# Runtime Performance\n- **Runtime**: `89 ms`  \n- **Beats**: `91.32%` of submissions\n\nThis high performance is due to not using array spread operations. The only issue is by not using the spread you are passing the sub-arrays by reference.\n\n# Intuition\nI approached this problem as a graph problem. Instead of using a stack I am using recursion to act as the stack.\n\nBy decrementing `n` I can easily control how deeply I flatten the array.\n\nThis solution avoids the use of array spreads (`...`), which is not a good approach. They are O(`n \xD7 d`) time compleixty where d is the depth. Spreads (`...`) are not constant time operations. \n\nAdditionally, spreads (`...`) will introduce noticable performance overhead in JavaScript. \n\nInstead, directly pushing elements into the result array minimizes unnecessary operations, ensuring consistently better runtime performance.\n\n# Approach\n1. Use recursion to traverse the nested arrays. This allows us to explore the structure deeply and handle the multi-dimensional nature naturally.\n2. Maintain a result array `res` to store flattened elements.\n3. For each element in the input array:\n   - If it\'s not an array or the depth has reached `0`, directly add it to the result.\n   - Otherwise, recursively call the helper function `dfs` with the subarray and decremented depth.\n4. Avoid using array spread (`...`) or `concat`, which introduces performance overhead.\n\n# Complexity\n- Time complexity - O(`n`):\nYou visit every element in the input array exactly once, regardless of how deeply nested it is, as long as the depth k is sufficient to fully flatten the array. Even for nested structures, each element contributes once to the traversal, making the time complexity linear with respect to the total number of elements, n.\n\n  Depth (k) does not directly affect the asymptotic time complexity, but for small k, fewer recursive calls occur, making the traversal faster in practice.\n\n- Space complexity:\n  - Auxiliary Space Complexity: O(`d`)\n    Space used by the recursion call stack.\n\nTechnically we are creating a new array to store the final flattened result and are using O(`n`). But in problems like this we are focusing on and want to minimize the auxiliary space complexity.\n\n# Code\n```typescript []\ntype MultiDimensionalArray = (number | MultiDimensionalArray)[];\n\nvar flat = function (arr:  MultiDimensionalArray, n: number):  MultiDimensionalArray {\n  const res: MultiDimensionalArray = []\n  \n  const dfs = (arr:  MultiDimensionalArray, n: number): void => {\n    for (const val of arr) {\n      if (!Array.isArray(val) || n === 0) {\n        res.push(val)\n      } else {\n        dfs(val, n-1)\n      }\n    }\n  }\n\n  dfs(arr, n)\n  return res\n};\n```
['TypeScript']