Create Components With Same Value — LeetCode 2440 Python Solution

HardTreeDepth-First SearchArrayMathEnumeration
Problem
#2440
Reading time
5 min

The problem

There is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] represents the value of the ith node.

Example

Input
nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]]
Output
2
Explanation
The above figure shows how we can delete the edges [0,1] and [3,4]. The created components are nodes [0], [1,2,3] and [4]. The sum of the values in each component equals 6. It can be proven that no better deletion exists, so the answer is 2.

Python solution

Python
class Solution:
    def componentValue(self, nums: List[int], edges: List[List[int]]) -> int:
        def dfs(i, fa):
            x = nums[i]
            for j in g[i]:
                if j != fa:
                    y = dfs(j, i)
                    if y == -1:
                        return -1
                    x += y
            if x > t:
                return -1
            return x if x < t else 0

        n = len(nums)
        g = defaultdict(list)
        for a, b in edges:
            g[a].append(b)
            g[b].append(a)
        s = sum(nums)
        mx = max(nums)
        for k in range(min(n, s // mx), 1, -1):
            if s % k == 0:
                t = s // k
                if dfs(0, -1) == 0:
                    return k - 1
        return 0

Complexity

MeasureComplexity
TimeO(n \times \sqrt{s}), where n and s are the length of nums and the sum of the values of all nodes in nums, respectively
SpaceO(V) auxiliary

Pattern: Tree Traversal

Choose the order — preorder, inorder, postorder, level — and the problem solves itself. LeetCode 2440. Create Components With Same Value is filed here because LeetCode tags it Tree, which is the vocabulary this hub collects.

The tree traversal guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2440. Create Components With Same Value?
LeetCode 2440. Create Components With Same Value is rated Hard on LeetCode.
What is the time complexity of LeetCode 2440. Create Components With Same Value?
The Python solution on this page runs in O(n \times \sqrt{s}), where n and s are the length of nums and the sum of the values of all nodes in nums, respectively.
What is the space complexity of LeetCode 2440. Create Components With Same Value?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 2440. Create Components With Same Value cover?
LeetCode 2440. Create Components With Same Value is tagged Tree, Depth-First Search, Array, Math and Enumeration on LeetCode.

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