Trains the technique from
LeetCode 339Nested List Weight SumThis 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.
An auditor hands you a file of ledger adjustments, grouped and regrouped by department. The file is supplied as JSON: entries is a list, and each slot in a list holds either a signed integer, which is one adjustment, or a further list, which is a nested group. A group is allowed to be empty.
The tier of an adjustment is the number of groups wrapped around it. Adjustments written straight into entries sit at tier 1, adjustments inside a group that sits straight in entries are at tier 2, and each further layer of nesting adds one.
Return the sum of value * tier over every adjustment the file contains.
Example 1
The adjustments 3 and 7 sit at tier 1, while 5 and -2 sit at tier 2, giving 3 + 7 + 2*5 + 2*(-2) = 16.
Example 2
Three groups wrap the single adjustment 4, so its tier is 3 and the total is 3*4 = 12.
Example 3
One tier-1 adjustment, and adjustments may be negative, so the total is 1*(-8) = -8.
Example 4
The tiers are 1 for 2, 2 for 3, 3 for -1 and 4 for 6, giving 2 + 2*3 + 3*(-1) + 4*6 = 29.
Example 5
The empty group holds no adjustments and contributes nothing, leaving the tier-1 adjustment 9.
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(entries: list) -> int:public int depthSum(List<NestedInteger> entries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.