FPEvalRepoPublic/LeetCodeMetaData
0402
1{2 "id": 2531,3 "name": "create-components-with-same-value",4 "difficulty": "Hard",5 "link": "https://leetcode.com/problems/create-components-with-same-value/",6 "date": "2022-10-01",7 "task_description": "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. You are also given 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 allowed to **delete** some edges, splitting the tree into multiple connected components. Let the **value** of a component be the sum of **all** `nums[i]` for which node `i` is in the component. Return_ the **maximum** number of edges you can delete, such that every connected component in the tree has the same value._ **Example 1:** ``` **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. ``` **Example 2:** ``` **Input:** nums = [2], edges = [] **Output:** 0 **Explanation:** There are no edges to be deleted. ``` **Constraints:** `1 <= n <= 2 * 104` `nums.length == n` `1 <= nums[i] <= 50` `edges.length == n - 1` `edges[i].length == 2` `0 <= edges[i][0], edges[i][1] <= n - 1` `edges` represents a valid tree.",8 "test_case": [9 {10 "label": "Example 1",11 "input": "nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]]",12 "output": "2 "13 },14 {15 "label": "Example 2",16 "input": "nums = [2], edges = []",17 "output": "0 "18 }19 ],20 "constraints": [21 "1 <= n <= 2 * 104",22 "nums.length == n",23 "1 <= nums[i] <= 50",24 "edges.length == n - 1",25 "edges[i].length == 2",26 "0 <= edges[i][0], edges[i][1] <= n - 1",27 "edges represents a valid tree."28 ],29 "python_template": "class Solution(object):\n def componentValue(self, nums, edges):\n \"\"\"\n :type nums: List[int]\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ",30 "java_template": "class Solution {\n public int componentValue(int[] nums, int[][] edges) {\n \n }\n}",31 "metadata": {32 "func_name": "componentValue"33 }34}