Nested List Weight Sum — LeetCode 339 Python Solution

MediumLeetCode PremiumDepth-First SearchBreadth-First Search
Problem
#339
Reading time
9 min

The 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.

Example

Input
nestedList = [[1,1],2,[1,1]]
Output
10
Explanation
Four 1's at depth 2, one 2 at depth 1. 1*2 + 1*2 + 2*1 + 1*2 + 1*2 = 10.

Python solution

Python
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
# class NestedInteger:
#    def __init__(self, value=None):
#        """
#        If value is not specified, initializes an empty list.
#        Otherwise initializes a single integer equal to value.
#        """
#
#    def isInteger(self):
#        """
#        @return True if this NestedInteger holds a single integer, rather than a nested list.
#        :rtype bool
#        """
#
#    def add(self, elem):
#        """
#        Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
#        :rtype void
#        """
#
#    def setInteger(self, value):
#        """
#        Set this NestedInteger to hold a single integer equal to value.
#        :rtype void
#        """
#
#    def getInteger(self):
#        """
#        @return the single integer that this NestedInteger holds, if it holds a single integer
#        Return None if this NestedInteger holds a nested list
#        :rtype int
#        """
#
#    def getList(self):
#        """
#        @return the nested list that this NestedInteger holds, if it holds a nested list
#        Return None if this NestedInteger holds a single integer
#        :rtype List[NestedInteger]
#        """
class Solution:
    def depthSum(self, nestedList: List[NestedInteger]) -> int:
        def dfs(nestedList, depth):
            depth_sum = 0
            for item in nestedList:
                if item.isInteger():
                    depth_sum += item.getInteger() * depth
                else:
                    depth_sum += dfs(item.getList(), depth + 1)
            return depth_sum

        return dfs(nestedList, 1)

Complexity

MeasureComplexity
TimeO(V+E)
SpaceO(V) auxiliary

Pattern: Breadth-First Search

Expand outward level by level, so the first time you arrive is the shortest way. LeetCode 339. Nested List Weight Sum is filed here because LeetCode tags it Breadth-First Search, which is the vocabulary this hub collects.

The breadth-first search guide has the Python template for the pattern and the 233 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 339. Nested List Weight Sum?
LeetCode 339. Nested List Weight Sum is rated Medium on LeetCode.
What is the time complexity of LeetCode 339. Nested List Weight Sum?
The Python solution on this page runs in O(V+E).
What is the space complexity of LeetCode 339. Nested List Weight Sum?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 339. Nested List Weight Sum cover?
LeetCode 339. Nested List Weight Sum is tagged Depth-First Search and Breadth-First Search on LeetCode.
Is LeetCode 339. Nested List Weight Sum a premium problem?
Yes. LeetCode 339. Nested List Weight Sum is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview