You're given a nested list of integers where each element can be either an integer or another list (which can contain integers or more nested lists). Think of it like Russian nesting dolls, but with numbers!
Here's the twist: instead of deeper elements having higher weights, they have lower weights. The weight of an integer equals maxDepth - depth + 1, where:
- depth = how many lists contain this integer
- maxDepth = the deepest level in the entire structure
Your goal is to return the sum of each integer multiplied by its weight.
Example: In [1,[4,[6]]], the depths are: 1(depth=1), 4(depth=2), 6(depth=3). Since maxDepth=3, the weights are: 1×3 + 4×2 + 6×1 = 17.
Input & Output
Visualization
Time & Space Complexity
Single DFS traversal of all n elements in the nested structure
Only recursion stack space proportional to maximum depth d
Constraints
- 1 ≤ nestedList.length ≤ 50
- The values of the integers in the nested list are in the range [-100, 100]
- The maximum depth of any integer is less than or equal to 50