leetcode - 2872. Maximum Number of K-Divisible Components

发布于:2025-02-11 ⋅ 阅读:(77) ⋅ 点赞:(0)

Description

There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.

You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the ith node, and an integer k.

A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by k, where the value of a connected component is the sum of the values of its nodes.

Return the maximum number of components in any valid split.

Example 1:
在这里插入图片描述

Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
Output: 2
Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because:
- The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.
- The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.
It can be shown that no other valid split has more than 2 connected components.

Example 2:
在这里插入图片描述

Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
Output: 3
Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:
- The value of the component containing node 0 is values[0] = 3.
- The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.
- The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.
It can be shown that no other valid split has more than 3 connected components.

Constraints:

1 <= n <= 3 * 10^4
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
values.length == n
0 <= values[i] <= 10^9
1 <= k <= 10^9
Sum of values is divisible by k.
The input is generated such that edges represents a valid tree.

Solution

Solved after hints…

We start with leaf nodes, and see if its value is divisible by k, if yes, then detach this leaf node, if no, then this leaf node has to be in the same component with its parent, so we just add its value to its parent’s value and detach it. After detaching, reduce its neighbor’s connect-degree.

Time complexity: ??
Space complexity: ??

Code

class Solution:
    def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int:
        def build_graph(n: int, edges: list) -> tuple:
            graph = {i: {} for i in range(n)}
            connect_degree = {i: 0 for i in range(n)}
            for node_a, node_b in edges:
                graph[node_a][node_b] = 1
                graph[node_b][node_a] = 1
                connect_degree[node_a] += 1
                connect_degree[node_b] += 1
            return graph, connect_degree
        # {node_i: {node_j: 1, node_k: 1, ...}}
        # {node_i: connect_degree}
        graph, connect_degree = build_graph(n, edges)
        queue = collections.deque([])
        # put all the nodes with 1 connected_degree in the queue\
        for node_i, c_degree in connect_degree.items():
            if c_degree == 1:
                queue.append(node_i)
        visited = set()
        res = 0
        while queue:
            node = queue.popleft()
            if node in visited:
                continue
            visited.add(node)
            for neighbor_node in graph[node]:
                connect_degree[neighbor_node] -= 1
                graph[neighbor_node].pop(node)
                if connect_degree[neighbor_node] == 1:
                    queue.append(neighbor_node)
            if values[node] % k == 0:
                res += 1
            else:
                neighbor_node, _ = graph[node].popitem()
                values[neighbor_node] += values[node]
        return max(1, res)

网站公告

今日签到

点亮在社区的每一天
去签到