✔️Easy Solution✔️2625. Flatten Deeply Nested Array✔️Level up🚀your JavaScript skills 🚀Day 22
Intuition\n Describe your first thoughts on how to solve this problem. \n>The problem requires us to flatten a multi-dimensional array based on a given depth. W
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n>The problem requires us to flatten a multi-dimensional array based on a given depth. We need to maintain the structure of the array up to the given depth and flatten the remaining subarrays.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo solve this problem, we can use an iterative approach with a stack.\n>1. Initialize a stack with the elements of the array along with their\ncorresponding depth levels. Each element is represented as a pair [item,\ndepth].\n>2. Initialize an empty array to store the flattened result.\n>3. Enter a while loop, which continues as long as there are elements in the\nstack.\n>4. Within each iteration of the loop, retrieve the top element [item, depth] from\nthe stack.\n>5. If the item is an array and the current depth `depth` is greater than 0, it\nmeans the element is a subarray that needs to be further flattened.\n>6. Iterate over the subarray, and for each element, push it back to the stack\nalong with a reduced depth level (depth - 1).\n>7. If the item is not an array or the depth is 0, it means the element is a nonarray item that should be directly added to the result array.\n>8. Continue the loop until there are no more elements in the stack.\n>9. Finally, return the flattened result array.\n\n\n ![image.png](https://assets.leetcode.com/users/images/b427e686-2e5d-469a-8e7a-db5140022a6b_1677715904.0948765.png)\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n```\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n>The time complexity of this approach is O(N), where N is the total number of elements in the input array. This is because we iterate over each element once, either to push it back to the stack or to add it to the result array.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n>The space complexity is also O(N), where N is the total number of elements in the input array. This is because in the worst case, all elements can be pushed onto the stack before being processed or added to the result array.\n\n# Code\n```JavaScript []\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function(arr, depth) {\n  const stack = [...arr.map(item => [item, depth])];\n  const result = [];\n\n  while (stack.length > 0) {\n    const [item, depth] = stack.pop();\n\n    if (Array.isArray(item) && depth > 0) {\n      stack.push(...item.map(subItem => [subItem, depth - 1]));\n    } else {\n      result.push(item);\n    }\n  }\n\n  return result.reverse();\n};\n\n\n```\n\n# Important topic to Learn \n\n| Sr No. | Topic | Sr No. | Topic |\n|-----|-----|-----|-----|\n1.|Arrays , Array methods() * |2.|Function programming *|\n3.|Higher-order function|4.|Memoization|\n5.|Currying|6.|Promises, async/await|\n7.|Compare Objects|8.|Prototypes, inheritance|\n\n>[ Note:- * marked is related to today\'s problem ]\n\n# Please Comment\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution comment below if you like it.\uD83D\uDE0A\n```
['JavaScript']