MediumNo. 2625

Flatten Deeply Nested Array — LeetCode 2625 Python Solution

Topics
JavaScript
Reading time
2 min

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

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0192Word FrequencyMedium
0193Valid Phone NumbersEasy
0194Transpose FileMedium
0195Tenth LineEasy
0228Summary RangesEasy

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.