> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Nested List Weight Sum II Python Solution

> Tested Python solution for LeetCode 364 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 364, [Medium](/catalog/medium). Topics: [Stack](/catalog/topics/stack), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search). [View on LeetCode](https://leetcode.com/problems/nested-list-weight-sum-ii/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 364   # by problem number
lcpy gen -s nested_list_weight_sum_ii   # by problem name
```

## Problem

You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists.

The **depth** of an integer is the number of lists that it is inside of. For example, the nested list `[1,[2,2],[[3],2],1]` has each integer's value set to its **depth**. Let `maxDepth` be the **maximum depth** of any integer.

The **weight** of an integer is `maxDepth - (the depth of the integer) + 1`.

Return *the sum of each integer in* `nestedList` *multiplied by its **weight***.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0364.Nested%20List%20Weight%20Sum%20II/images/nestedlistweightsumiiex1.png)

```
Input: nestedList = [[1,1],2,[1,1]]
Output: 8
Explanation: Four 1's with a weight of 1, one 2 with a weight of 2.
1*1 + 1*1 + 2*2 + 1*1 + 1*1 = 8
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0364.Nested%20List%20Weight%20Sum%20II/images/nestedlistweightsumiiex2.png)

```
Input: nestedList = [1,[4,[6]]]
Output: 17
Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at depth 1.
1*3 + 4*2 + 6*1 = 17
```

### Constraints

* `1 <= nestedList.length <= 50`
* The values of the integers in the nested list is in the range `[-100, 100]`.
* The maximum **depth** of any integer is less than or equal to `50`.
* There are no empty lists.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/nested_list_weight_sum_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/nested_list_weight_sum_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from typing import Any


class Solution:
    # Time: O(n) — every integer visited once (n = total elements)
    # Space: O(d) — recursion depth equals nesting depth
    def depth_sum_inverse(self, nested_list: list[Any]) -> int:
        def dfs(items: list[Any], depth: int) -> tuple[int, int, int]:
            plain = 0
            by_depth = 0
            max_depth = depth
            for item in items:
                if isinstance(item, int):
                    plain += item
                    by_depth += item * depth
                else:
                    p, b, md = dfs(item, depth + 1)
                    plain += p
                    by_depth += b
                    max_depth = max(max_depth, md)
            return plain, by_depth, max_depth

        plain, by_depth, max_depth = dfs(nested_list, 1)
        # sum(v * (max_depth - d + 1)) = (max_depth + 1) * sum(v) - sum(v * d)
        return (max_depth + 1) * plain - by_depth
```

## Complexity

| Time | Space |
| - | - |
| O(n) — every integer visited once (n = total elements) | O(d) — recursion depth equals nesting depth |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.