diff --git a/solution/2800-2899/2869.Minimum Operations to Collect Elements/README.md b/solution/2800-2899/2869.Minimum Operations to Collect Elements/README.md new file mode 100644 index 0000000000000..5989f13b3c5de --- /dev/null +++ b/solution/2800-2899/2869.Minimum Operations to Collect Elements/README.md @@ -0,0 +1,111 @@ +# [2869. Minimum Operations to Collect Elements](https://leetcode.cn/problems/minimum-operations-to-collect-elements/) + +[English Version](/solution/2800-2899/2869.Minimum%20Operations%20to%20Collect%20Elements/README_EN.md) + +## 题目描述 + + + +

一次操作中,你可以将数组的最后一个元素删除,将该元素添加到一个集合中。

+ +

请你返回收集元素 1, 2, ..., k 需要的 最少操作次数 。

+ +

 

+ +

示例 1:

+ +
输入:nums = [3,1,5,4,2], k = 2
+输出:4
+解释:4 次操作后,集合中的元素依次添加了 2 ,4 ,5 和 1 。此时集合中包含元素 1 和 2 ,所以答案为 4 。
+
+ +

示例 2:

+ +
输入:nums = [3,1,5,4,2], k = 5
+输出:5
+解释:5 次操作后,集合中的元素依次添加了 2 ,4 ,5 ,1 和 3 。此时集合中包含元素 1 到 5 ,所以答案为 5 。
+
+ +

示例 3:

+ +
输入:nums = [3,2,5,3,1], k = 3
+输出:4
+解释:4 次操作后,集合中的元素依次添加了 1 ,3 ,5 和 2 。此时集合中包含元素 1 到 3  ,所以答案为 4 。
+
+ +

 

+ +

提示:

+ + + +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java +class Solution { + public int minOperations(List nums, int k) { + boolean[] isAdded = new boolean[k]; + int n = nums.size(); + int count = 0; + for (int i = n - 1; i >= 0; i--) { + if (nums.get(i) > k || isAdded[nums.get(i) - 1]) { + continue; + } + isAdded[nums.get(i) - 1] = true; + count++; + if (count == k) { + return n - i; + } + } + return n; + } +} + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2869.Minimum Operations to Collect Elements/README_EN.md b/solution/2800-2899/2869.Minimum Operations to Collect Elements/README_EN.md new file mode 100644 index 0000000000000..beb63278bcdb3 --- /dev/null +++ b/solution/2800-2899/2869.Minimum Operations to Collect Elements/README_EN.md @@ -0,0 +1,101 @@ +# [2869. Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements/) + +[中文文档](/solution/2800-2899/2869.Minimum%20Operations%20to%20Collect%20Elements/README.md) + +## Description + +

In one operation, you can remove the last element of the array and add it to your collection.

+ +

Return the minimum number of operations needed to collect elements 1, 2, ..., k.

+ +

 

+

Example 1:

+ +
Input: nums = [3,1,5,4,2], k = 2
+Output: 4
+Explanation: After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4.
+
+ +

Example 2:

+ +
Input: nums = [3,1,5,4,2], k = 5
+Output: 5
+Explanation: After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5.
+
+ +

Example 3:

+ +
Input: nums = [3,2,5,3,1], k = 3
+Output: 4
+Explanation: After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4.
+
+ +

 

+

Constraints:

+ + + +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java +class Solution { + public int minOperations(List nums, int k) { + boolean[] isAdded = new boolean[k]; + int n = nums.size(); + int count = 0; + for (int i = n - 1; i >= 0; i--) { + if (nums.get(i) > k || isAdded[nums.get(i) - 1]) { + continue; + } + isAdded[nums.get(i) - 1] = true; + count++; + if (count == k) { + return n - i; + } + } + return n; + } +} + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2869.Minimum Operations to Collect Elements/Solution.java b/solution/2800-2899/2869.Minimum Operations to Collect Elements/Solution.java new file mode 100644 index 0000000000000..a2c42b4bde54d --- /dev/null +++ b/solution/2800-2899/2869.Minimum Operations to Collect Elements/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int minOperations(List nums, int k) { + boolean[] isAdded = new boolean[k]; + int n = nums.size(); + int count = 0; + for (int i = n - 1; i >= 0; i--) { + if (nums.get(i) > k || isAdded[nums.get(i) - 1]) { + continue; + } + isAdded[nums.get(i) - 1] = true; + count++; + if (count == k) { + return n - i; + } + } + return n; + } +} diff --git a/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README.md b/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README.md new file mode 100644 index 0000000000000..a6ffda2bf7b15 --- /dev/null +++ b/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README.md @@ -0,0 +1,119 @@ +# [2870. Minimum Number of Operations to Make Array Empty](https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-empty/) + +[English Version](/solution/2800-2899/2870.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20Empty/README_EN.md) + +## 题目描述 + + + +

你可以对数组执行以下两种操作 任意次 :

+ +
    +
  • 从数组中选择 两个 值 相等 的元素,并将它们从数组中 删除 。
  • +
  • 从数组中选择 三个 值 相等 的元素,并将它们从数组中 删除 。
  • +
+ +

请你返回使数组为空的 最少 操作次数,如果无法达成,请返回 -1 。

+ +

 

+ +

示例 1:

+ +
输入:nums = [2,3,3,2,2,4,2,3,4]
+输出:4
+解释:我们可以执行以下操作使数组为空:
+- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。
+- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。
+- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。
+- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。
+至少需要 4 步操作使数组为空。
+
+ +

示例 2:

+ +
输入:nums = [2,1,2,2,3,3]
+输出:-1
+解释:无法使数组为空。
+
+ +

 

+ +

提示:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java +class Solution { + public int minOperations(int[] nums) { + Map count = new HashMap<>(); + for (int num : nums) { + // count.put(num, count.getOrDefault(num, 0) + 1); + count.merge(num, 1, Integer::sum); + } + int ans = 0; + for (Integer c : count.values()) { + if (c < 2) { + return -1; + } + int r = c % 3; + int d = c / 3; + switch (r) { + case (0) -> { + ans += d; + } + default -> { + ans += d + 1; + } + } + } + return ans; + } +} +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README_EN.md b/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README_EN.md new file mode 100644 index 0000000000000..f1e4f0c13fc85 --- /dev/null +++ b/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README_EN.md @@ -0,0 +1,109 @@ +# [2870. Minimum Number of Operations to Make Array Empty](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/) + +[中文文档](/solution/2800-2899/2870.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20Empty/README.md) + +## Description + +

There are two types of operations that you can apply on the array any number of times:

+ +
    +
  • Choose two elements with equal values and delete them from the array.
  • +
  • Choose three elements with equal values and delete them from the array.
  • +
+ +

Return the minimum number of operations required to make the array empty, or -1 if it is not possible.

+ +

 

+

Example 1:

+ +
Input: nums = [2,3,3,2,2,4,2,3,4]
+Output: 4
+Explanation: We can apply the following operations to make the array empty:
+- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
+- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
+- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
+- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
+It can be shown that we cannot make the array empty in less than 4 operations.
+
+ +

Example 2:

+ +
Input: nums = [2,1,2,2,3,3]
+Output: -1
+Explanation: It is impossible to empty the array.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 106
  • +
+ +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java +class Solution { + public int minOperations(int[] nums) { + Map count = new HashMap<>(); + for (int num : nums) { + // count.put(num, count.getOrDefault(num, 0) + 1); + count.merge(num, 1, Integer::sum); + } + int ans = 0; + for (Integer c : count.values()) { + if (c < 2) { + return -1; + } + int r = c % 3; + int d = c / 3; + switch (r) { + case (0) -> { + ans += d; + } + default -> { + ans += d + 1; + } + } + } + return ans; + } +} +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/Solution.java b/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/Solution.java new file mode 100644 index 0000000000000..24479cba54328 --- /dev/null +++ b/solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/Solution.java @@ -0,0 +1,26 @@ +class Solution { + public int minOperations(int[] nums) { + Map count = new HashMap<>(); + for (int num : nums) { + // count.put(num, count.getOrDefault(num, 0) + 1); + count.merge(num, 1, Integer::sum); + } + int ans = 0; + for (Integer c : count.values()) { + if (c < 2) { + return -1; + } + int r = c % 3; + int d = c / 3; + switch (r) { + case (0) -> { + ans += d; + } + default -> { + ans += d + 1; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README.md b/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README.md new file mode 100644 index 0000000000000..34a6162d75242 --- /dev/null +++ b/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README.md @@ -0,0 +1,112 @@ +# [2871. Split Array Into Maximum Number of Subarrays](https://leetcode.cn/problems/split-array-into-maximum-number-of-subarrays/) + +[English Version](/solution/2800-2899/2871.Split%20Array%20Into%20Maximum%20Number%20of%20Subarrays/README_EN.md) + +## 题目描述 + + + +

我们定义满足 l <= r 的子数组 nums[l..r] 的分数为 nums[l] AND nums[l + 1] AND ... AND nums[r] ,其中 AND 是按位与运算。

+ +

请你将数组分割成一个或者更多子数组,满足:

+ +
    +
  • 每个 元素都  属于一个子数组。
  • +
  • 子数组分数之和尽可能 小 。
  • +
+ +

请你在满足以上要求的条件下,返回 最多 可以得到多少个子数组。

+ +

一个 子数组 是一个数组中一段连续的元素。

+ +

 

+ +

示例 1:

+ +
输入:nums = [1,0,2,0,1,2]
+输出:3
+解释:我们可以将数组分割成以下子数组:
+- [1,0] 。子数组分数为 1 AND 0 = 0 。
+- [2,0] 。子数组分数为 2 AND 0 = 0 。
+- [1,2] 。子数组分数为 1 AND 2 = 0 。
+分数之和为 0 + 0 + 0 = 0 ,是我们可以得到的最小分数之和。
+在分数之和为 0 的前提下,最多可以将数组分割成 3 个子数组。所以返回 3 。
+
+ +

示例 2:

+ +
输入:nums = [5,7,1,3]
+输出:1
+解释:我们可以将数组分割成一个子数组:[5,7,1,3] ,分数为 1 ,这是可以得到的最小总分数。
+在总分数为 1 的前提下,最多可以将数组分割成 1 个子数组。所以返回 1 。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 106
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java +class Solution { + public int maxSubarrays(int[] nums) { + int score = -1; + int ans = 1; + for (int num : nums) { + score &= num; + if (score == 0) { + ans++; + score = -1; + } + } + return ans == 1 ? 1 : ans - 1; + } +} +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README_EN.md b/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README_EN.md new file mode 100644 index 0000000000000..bb225e009d802 --- /dev/null +++ b/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README_EN.md @@ -0,0 +1,102 @@ +# [2871. Split Array Into Maximum Number of Subarrays](https://leetcode.com/problems/split-array-into-maximum-number-of-subarrays/) + +[中文文档](/solution/2800-2899/2871.Split%20Array%20Into%20Maximum%20Number%20of%20Subarrays/README.md) + +## Description + +

We define the score of subarray nums[l..r] such that l <= r as nums[l] AND nums[l + 1] AND ... AND nums[r] where AND is the bitwise AND operation.

+ +

Consider splitting the array into one or more subarrays such that the following conditions are satisfied:

+ +
    +
  • Each element of the array belongs to exactly one subarray.
  • +
  • The sum of scores of the subarrays is the minimum possible.
  • +
+ +

Return the maximum number of subarrays in a split that satisfies the conditions above.

+ +

A subarray is a contiguous part of an array.

+ +

 

+

Example 1:

+ +
Input: nums = [1,0,2,0,1,2]
+Output: 3
+Explanation: We can split the array into the following subarrays:
+- [1,0]. The score of this subarray is 1 AND 0 = 0.
+- [2,0]. The score of this subarray is 2 AND 0 = 0.
+- [1,2]. The score of this subarray is 1 AND 2 = 0.
+The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain.
+It can be shown that we cannot split the array into more than 3 subarrays with a total score of 0. So we return 3.
+
+ +

Example 2:

+ +
Input: nums = [5,7,1,3]
+Output: 1
+Explanation: We can split the array into one subarray: [5,7,1,3] with a score of 1, which is the minimum possible score that we can obtain.
+It can be shown that we cannot split the array into more than 1 subarray with a total score of 1. So we return 1.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 106
  • +
+ +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java +class Solution { + public int maxSubarrays(int[] nums) { + int score = -1; + int ans = 1; + for (int num : nums) { + score &= num; + if (score == 0) { + ans++; + score = -1; + } + } + return ans == 1 ? 1 : ans - 1; + } +} +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/Solution.java b/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/Solution.java new file mode 100644 index 0000000000000..b217f65178267 --- /dev/null +++ b/solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int maxSubarrays(int[] nums) { + int score = -1; + int ans = 1; + for (int num : nums) { + score &= num; + if (score == 0) { + ans++; + score = -1; + } + } + return ans == 1 ? 1 : ans - 1; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md new file mode 100644 index 0000000000000..c49e4de881819 --- /dev/null +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md @@ -0,0 +1,138 @@ +# [2872. Maximum Number of K-Divisible Components](https://leetcode.com/problems/maximum-number-of-k-divisible-components/) + +[English Version](/solution/2800-2899/2872.Maximum%20Number%20of%20K-Divisible%20Components/README_EN.md) + +## 题目描述 + + + +

给你一棵 n 个节点的无向树,节点编号为 0 到 n - 1 。给你整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。

+ +

同时给你一个下标从 0 开始长度为 n 的整数数组 values ,其中 values[i] 是第 i 个节点的  。再给你一个整数 k 。

+ +

你可以从树中删除一些边,也可以一条边也不删,得到若干连通块。一个 连通块的值 定义为连通块中所有节点值之和。如果所有连通块的值都可以被 k 整除,那么我们说这是一个 合法分割 。

+ +

请你返回所有合法分割中,连通块数目的最大值 。

+ +

 

+ +

示例 1:

+ +

+ +
输入:n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
+输出:2
+解释:我们删除节点 1 和 2 之间的边。这是一个合法分割,因为:
+- 节点 1 和 3 所在连通块的值为 values[1] + values[3] = 12 。
+- 节点 0 ,2 和 4 所在连通块的值为 values[0] + values[2] + values[4] = 6 。
+最多可以得到 2 个连通块的合法分割。
+ +

示例 2:

+ +

+ +
输入: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
+输出:3
+解释:我们删除节点 0 和 2 ,以及节点 0 和 1 之间的边。这是一个合法分割,因为:
+- 节点 0 的连通块的值为 values[0] = 3 。
+- 节点 2 ,5 和 6 所在连通块的值为 values[2] + values[5] + values[6] = 9 。
+- 节点 1 ,3 和 4 的连通块的值为 values[1] + values[3] + values[4] = 6 。
+最多可以得到 3 个连通块的合法分割。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= n <= 3 * 104
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • values.length == n
  • +
  • 0 <= values[i] <= 109
  • +
  • 1 <= k <= 109
  • +
  • values 之和可以被 k 整除。
  • +
  • 输入保证 edges 是一棵无向树。
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java +class Solution { + int n, k; + int[] values; + int[] dfs(int curr, int parent, List> adj) { + int[] res = new int[] {0, values[curr] % k}; + for (int next : adj.get(curr)) { + if (next == parent) { + continue; + } + int[] update = dfs(next, curr, adj); + res[0] += update[0]; + res[1] += update[1]; + } + res[1] %= k; + res[0] += res[1] == 0 ? 1 : 0; + return res; + } + public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) { + this.n = n; + this.k = k; + this.values = values; + List> adj = new ArrayList<>(); + int[][] dp = new int[n][2]; + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + for (int[] edge : edges) { + adj.get(edge[0]).add(edge[1]); + adj.get(edge[1]).add(edge[0]); + } + int[] ans = dfs(0, -1, adj); + return ans[1] == 0 ? ans[0] : 0; + } +} +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md new file mode 100644 index 0000000000000..65b50f2aa42ff --- /dev/null +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md @@ -0,0 +1,124 @@ +# [2872. Maximum Number of K-Divisible Components](https://leetcode.com/problems/maximum-number-of-k-divisible-components/) + +[中文文档](/solution/2800-2899/2872.Maximum%20Number%20of%20K-Divisible%20Components/README.md) + +## 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 * 104
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 0 <= ai, bi < n
  • +
  • values.length == n
  • +
  • 0 <= values[i] <= 109
  • +
  • 1 <= k <= 109
  • +
  • Sum of values is divisible by k.
  • +
  • The input is generated such that edges represents a valid tree.
  • +
+ +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java +class Solution { + int n, k; + int[] values; + int[] dfs(int curr, int parent, List> adj) { + int[] res = new int[] {0, values[curr] % k}; + for (int next : adj.get(curr)) { + if (next == parent) { + continue; + } + int[] update = dfs(next, curr, adj); + res[0] += update[0]; + res[1] += update[1]; + } + res[1] %= k; + res[0] += res[1] == 0 ? 1 : 0; + return res; + } + public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) { + this.n = n; + this.k = k; + this.values = values; + List> adj = new ArrayList<>(); + int[][] dp = new int[n][2]; + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + for (int[] edge : edges) { + adj.get(edge[0]).add(edge[1]); + adj.get(edge[1]).add(edge[0]); + } + int[] ans = dfs(0, -1, adj); + return ans[1] == 0 ? ans[0] : 0; + } +} +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **TypeScript** + +```ts + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.java b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.java new file mode 100644 index 0000000000000..c785cb5e31546 --- /dev/null +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.java @@ -0,0 +1,34 @@ +class Solution { + int n, k; + int[] values; + int[] dfs(int curr, int parent, List> adj) { + int[] res = new int[] {0, values[curr] % k}; + for (int next : adj.get(curr)) { + if (next == parent) { + continue; + } + int[] update = dfs(next, curr, adj); + res[0] += update[0]; + res[1] += update[1]; + } + res[1] %= k; + res[0] += res[1] == 0 ? 1 : 0; + return res; + } + public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) { + this.n = n; + this.k = k; + this.values = values; + List> adj = new ArrayList<>(); + int[][] dp = new int[n][2]; + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + for (int[] edge : edges) { + adj.get(edge[0]).add(edge[1]); + adj.get(edge[1]).add(edge[0]); + } + int[] ans = dfs(0, -1, adj); + return ans[1] == 0 ? ans[0] : 0; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/images/example1.jpg b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/images/example1.jpg new file mode 100644 index 0000000000000..eec9789bd2ee5 Binary files /dev/null and b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/images/example1.jpg differ diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/images/example2.jpg b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/images/example2.jpg new file mode 100644 index 0000000000000..785149ce8bbf9 Binary files /dev/null and b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/images/example2.jpg differ