Practice/Meta/Leetcode 339. Nested List Weight Sum
CodingMust
You are given a structure that contains integers and nested lists. Each element can be either an integer or a list that may contain more integers or lists, creating arbitrary levels of nesting.
Calculate the weighted sum where each integer contributes to the total based on its nesting depth. An integer at depth 1 (top level) contributes its value multiplied by 1, an integer at depth 2 contributes its value multiplied by 2, and so on.
For example, in the structure [1, [2, 3]], the integer 1 is at depth 1 and contributes 1×1=1, while integers 2 and 3 are at depth 2 and contribute 2×2=4 and 3×2=6 respectively, for a total of 11.
Example 1:
Input: [[1,1],2,[1,1]] Output: 10 Explanation: Four 1's at depth 2 contribute 4*2=8, and one 2 at depth 1 contributes 2*1=2. Total: 8+2=10
Example 2:
` Input: [1,[4,[6]]] Output: 27 Explanation:
Example 3:
Input: [1,2,3,4] Output: 10 Explanation: All integers are at depth 1: (1+2+3+4)*1 = 10