Trains the technique from
LeetCode 364Nested List Weight Sum IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A parts file arrives as nestedList, a list whose entries are each either a whole number or another list built the same way. No list anywhere in the file is empty.
The level of an entry is how many lists have to be opened to reach it. An entry written directly inside nestedList is at level 1, an entry inside a list that is itself inside nestedList is at level 2, and so on. Write D for the largest level at which a whole number appears anywhere in the file.
The weight of a whole number that sits at level L is D - L + 1. So the whole numbers buried deepest carry weight 1, those one level up carry weight 2, and those written at level 1 carry weight D.
Return the sum, over every whole number in the file, of that number multiplied by its weight.
Example 1
The number 2 sits at level 1, 5 at level 2 and 7 at level 3, so D is 3. The weights are then 3, 2 and 1, which gives 6 plus 10 plus 7.
Example 2
Every number sits at level 1 and D is 1, so each weight is 1 and the answer is just 7 + 3 - 2 = 8.
Example 3
Here D is 2. The number -100 sits at level 1 with weight 2 and 100 sits at level 2 with weight 1, so the answer is -200 + 100 = -100.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def depth_sum_inverse(nestedList: list) -> int:public int depthSumInverse(List<NestedInteger> nestedList)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.