99% Easy DFS recursive solution with explanations ✅
Hello everyone! So, I\'ve come up with a solution to resolve this problem, by the way i\'m new to JS so if you have any advice you\'re welcome. \uD83D\uDE01\n\n
**Hello everyone! So, I\'ve come up with a solution to resolve this problem, by the way i\'m new to JS so if you have any advice you\'re welcome. \uD83D\uDE01**\n\n# Intuition\nWhich algorithm can i use to keep tracking the deep ?`\uD83D\uDCA1DFS`\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFirstly, I\'ve instantiated an array called ans, which will serve as a container for elements after they\'ve been flattened. It essentially acts as a receptacle for our final data.\n\nNext, I\'ve defined a function called dfs, which stands for "Depth First Search." This function lies at the core of the process of traversing arrays to extract elements for flattening.\n\nThe dfs function comprises three parameters: depth (representing the current depth), maxDepth (defining the maximum depth of traversal), and currArray (the array currently under exploration).\n\nNow, the conditional statement is where the intrigue lies. It checks if the current depth depth matches the maximum depth maxDepth. If true, it signifies that we\'ve reached the depth limit, so we add currArray (the array we\'ve explored) to our container ans and we stop the depth exploration.\n\nHowever, if we haven\'t reached the maximum depth yet, we enter a for...of loop. This acts like a machine to inspect each element within currArray.\n\nIf the element we\'re looking at is itself an array, it means we can dig even deeper. Thus, we recursively call the dfs function, incrementing the depth by 1 and passing the new element as currArray.\n\nIn the case where the element isn\'t an array, it simply indicates an ordinary value. In this case, we directly add that value to our container ans.\n\nFinally, the main flat function calls the dfs function with an initial depth of **-1** (to indicate we haven\'t started yet), the maximum depth **n**, and the initial input array **arr**.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n\n**m** = number of elements in arr\n\n**n** = maximum stack calls used by the recursion\n\n- Time complexity: **O(n)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:  **O(max(n, m))**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n    var ans= [];\n\n    const dfs = (depth,maxDepth,currArray) =>{\n        if(depth==maxDepth){\n            ans.push(currArray);\n            return;\n        }\n\n        for(element of currArray){\n            if(Array.isArray(element)){\n                dfs(depth+1,maxDepth,element);\n            }else{\n                ans.push(element);\n            }\n        }\n    };\n\n    dfs(-1,n,arr);\n\n    return ans;\n};\n```
['Depth-First Search', 'Recursion', 'JavaScript']