Nested List Weight Sum
You are given a nested list of integers where each element is either a single integer or a list whose elements may themselves be integers or lists. The depth of an integer is the number of lists that contain it, starting at 1 for the outermost level. Return the sum of every integer multiplied by its depth.
Open official problem prompt ↗Add up every integer in an arbitrarily nested list, but weight each one by how many lists enclose it.
Think of a company org chart: an employee's influence counts more the deeper their department is nested. You walk down each branch, and the further you descend, the higher the multiplier on the numbers you find there.
- Input
- nestedList = [[1,1],2,[1,1]]
- Output
- 10
- Why
- Four 1's sit at depth 2 and the 2 sits at depth 1, so 4*(1*2) + 1*(2*1) = 8 + 2 = 10.
1 <= nestedList.length <= 50The values of the integers are in the range [-100, 100]The maximum depth of any integer is <= 50