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 。 ++ +
+ +
提示:
+ +1 <= nums.length <= 50
1 <= nums[i] <= nums.length
1 <= k <= nums.length
1, 2, ..., k
。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:
+ +1 <= nums.length <= 50
1 <= nums[i] <= nums.length
1 <= k <= nums.length
1, 2, ..., k
.你可以对数组执行以下两种操作 任意次 :
+ +请你返回使数组为空的 最少 操作次数,如果无法达成,请返回 -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
There are two types of operations that you can apply on the array any number of times:
+ +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
我们定义满足 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
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:
+ +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
给你一棵 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
是一棵无向树。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
values
is divisible by k
.edges
represents a valid tree.