Flatten Deeply Nested Array — LeetCode 2625 Python Solution
MediumJavaScript
- Problem
- #2625
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
- 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.
Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2625. Flatten Deeply Nested Array?
- LeetCode 2625. Flatten Deeply Nested Array is rated Medium on LeetCode.
- What topics does LeetCode 2625. Flatten Deeply Nested Array cover?
- LeetCode 2625. Flatten Deeply Nested Array is tagged JavaScript on LeetCode.