b {
- return a
+ if ans < t {
+ ans = t
+ }
}
- return b
+ return
}
\ No newline at end of file
diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.java b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.java
index fde57e3d59128..4dd46105d548c 100644
--- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.java
+++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.java
@@ -1,23 +1,22 @@
class Solution {
- private int ans;
- public int maximumRows(int[][] mat, int cols) {
- int m = mat.length, n = mat[0].length;
- int[] arr = new int[m];
+ public int maximumRows(int[][] matrix, int numSelect) {
+ int m = matrix.length, n = matrix[0].length;
+ int[] rows = new int[m];
for (int i = 0; i < m; ++i) {
- int x = 0;
for (int j = 0; j < n; ++j) {
- x |= mat[i][j] << j;
+ if (matrix[i][j] == 1) {
+ rows[i] |= 1 << j;
+ }
}
- arr[i] = x;
}
int ans = 0;
- for (int mask = 1; mask <= 1 << n; ++mask) {
- if (Integer.bitCount(mask) > cols) {
+ for (int mask = 1; mask < 1 << n; ++mask) {
+ if (Integer.bitCount(mask) != numSelect) {
continue;
}
int t = 0;
- for (int v : arr) {
- if ((v & mask) == v) {
+ for (int x : rows) {
+ if ((x & mask) == x) {
++t;
}
}
diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.py b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.py
index 1baf019f39150..f9bd7a7ee7a08 100644
--- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.py
+++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.py
@@ -1,15 +1,14 @@
class Solution:
- def maximumRows(self, mat: List[List[int]], cols: int) -> int:
- arr = []
- for i, row in enumerate(mat):
- x = 0
- for j, v in enumerate(row):
- x |= v << j
- arr.append(x)
- ans, n = 0, len(mat[0])
- for mask in range(1, 1 << n | 1):
- if mask.bit_count() > cols:
+ def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int:
+ rows = []
+ for row in matrix:
+ mask = reduce(or_, (1 << j for j, x in enumerate(row) if x), 0)
+ rows.append(mask)
+
+ ans = 0
+ for mask in range(1 << len(matrix[0])):
+ if mask.bit_count() != numSelect:
continue
- t = sum((v & mask) == v for v in arr)
+ t = sum((x & mask) == x for x in rows)
ans = max(ans, t)
return ans
diff --git a/solution/2500-2599/2539.Count the Number of Good Subsequences/README.md b/solution/2500-2599/2539.Count the Number of Good Subsequences/README.md
index 89b789dd5c681..faed8ea59b446 100644
--- a/solution/2500-2599/2539.Count the Number of Good Subsequences/README.md
+++ b/solution/2500-2599/2539.Count the Number of Good Subsequences/README.md
@@ -170,25 +170,22 @@ long qmi(long a, long k, long p) {
return res;
}
-void init() {
+int init = []() {
f[0] = 1;
g[0] = 1;
for (int i = 1; i < N; ++i) {
f[i] = f[i - 1] * i % MOD;
g[i] = qmi(f[i], MOD - 2, MOD);
}
-}
+ return 0;
+}();
int comb(int n, int k) {
return (f[n] * g[k] % MOD) * g[n - k] % MOD;
}
-
class Solution {
public:
- Solution() {
- init();
- }
int countGoodSubsequences(string s) {
int cnt[26]{};
int mx = 1;
diff --git a/solution/2500-2599/2539.Count the Number of Good Subsequences/README_EN.md b/solution/2500-2599/2539.Count the Number of Good Subsequences/README_EN.md
index f95868cead14b..4ee42cad9d401 100644
--- a/solution/2500-2599/2539.Count the Number of Good Subsequences/README_EN.md
+++ b/solution/2500-2599/2539.Count the Number of Good Subsequences/README_EN.md
@@ -150,25 +150,22 @@ long qmi(long a, long k, long p) {
return res;
}
-void init() {
+int init = []() {
f[0] = 1;
g[0] = 1;
for (int i = 1; i < N; ++i) {
f[i] = f[i - 1] * i % MOD;
g[i] = qmi(f[i], MOD - 2, MOD);
}
-}
+ return 0;
+}();
int comb(int n, int k) {
return (f[n] * g[k] % MOD) * g[n - k] % MOD;
}
-
class Solution {
public:
- Solution() {
- init();
- }
int countGoodSubsequences(string s) {
int cnt[26]{};
int mx = 1;
diff --git a/solution/2500-2599/2539.Count the Number of Good Subsequences/Solutoin.cpp b/solution/2500-2599/2539.Count the Number of Good Subsequences/Solutoin.cpp
index 828674d2b6614..2f58bd17aa7c5 100644
--- a/solution/2500-2599/2539.Count the Number of Good Subsequences/Solutoin.cpp
+++ b/solution/2500-2599/2539.Count the Number of Good Subsequences/Solutoin.cpp
@@ -15,14 +15,15 @@ long qmi(long a, long k, long p) {
return res;
}
-void init() {
+int init = []() {
f[0] = 1;
g[0] = 1;
for (int i = 1; i < N; ++i) {
f[i] = f[i - 1] * i % MOD;
g[i] = qmi(f[i], MOD - 2, MOD);
}
-}
+ return 0;
+}();
int comb(int n, int k) {
return (f[n] * g[k] % MOD) * g[n - k] % MOD;
@@ -30,9 +31,6 @@ int comb(int n, int k) {
class Solution {
public:
- Solution() {
- init();
- }
int countGoodSubsequences(string s) {
int cnt[26]{};
int mx = 1;
diff --git a/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md b/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md
index 6bf65e668d8ad..40cca6f1872c3 100644
--- a/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md
+++ b/solution/2500-2599/2591.Distribute Money to Maximum Children/README_EN.md
@@ -124,7 +124,6 @@ func distMoney(money int, children int) int {
}
```
-
### **TypeScript**
```ts
diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/README.md b/solution/2600-2699/2600.K Items With the Maximum Sum/README.md
new file mode 100644
index 0000000000000..64ca11a95d456
--- /dev/null
+++ b/solution/2600-2699/2600.K Items With the Maximum Sum/README.md
@@ -0,0 +1,144 @@
+# [2600. K 件物品的最大和](https://leetcode.cn/problems/k-items-with-the-maximum-sum)
+
+[English Version](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README_EN.md)
+
+## 题目描述
+
+
+
+袋子中装有一些物品,每个物品上都标记着数字 1
、0
或 -1
。
+
+给你四个非负整数 numOnes
、numZeros
、numNegOnes
和 k
。
+
+袋子最初包含:
+
+
+ numOnes
件标记为 1
的物品。
+ numZeroes
件标记为 0
的物品。
+ numNegOnes
件标记为 -1
的物品。
+
+
+现计划从这些物品中恰好选出 k
件物品。返回所有可行方案中,物品上所标记数字之和的最大值。
+
+
+
+示例 1:
+
+输入:numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
+输出:2
+解释:袋子中的物品分别标记为 {1, 1, 1, 0, 0} 。取 2 件标记为 1 的物品,得到的数字之和为 2 。
+可以证明 2 是所有可行方案中的最大值。
+
+示例 2:
+
+输入:numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
+输出:3
+解释:袋子中的物品分别标记为 {1, 1, 1, 0, 0} 。取 3 件标记为 1 的物品,1 件标记为 0 的物品,得到的数字之和为 3 。
+可以证明 3 是所有可行方案中的最大值。
+
+
+
+
+提示:
+
+
+ 0 <= numOnes, numZeros, numNegOnes <= 50
+ 0 <= k <= numOnes + numZeros + numNegOnes
+
+
+## 解法
+
+
+
+**方法一:贪心**
+
+根据题目描述,我们应该尽可能多地取标记为 $1$ 的物品,然后取标记为 $0$ 的物品,最后取标记为 $-1$ 的物品。
+
+因此:
+
+- 如果袋子中的物品标记为 $1$ 的数量大于等于 $k$,那么取 $k$ 件物品,数字之和为 $k$;
+- 如果袋子中的物品标记为 $1$ 的数量小于 $k$,那么取 $numOnes$ 件物品,数字之和为 $numOnes$;如果标记为 $0$ 的物品数量大于等于 $k - numOnes$,那么再取 $k - numOnes$ 件物品,数字之和还是 $numOnes$;
+- 否则,我们再从标记为 $-1$ 的物品中取 $k - numOnes - numZeros$ 件物品,数字之和为 $numOnes - (k - numOnes - numZeros)$。
+
+时间复杂度 $O(1)$,空间复杂度 $O(1)$。
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:
+ if numOnes >= k:
+ return k
+ k -= numOnes
+ if numZeros >= k:
+ return numOnes
+ k -= numZeros
+ return numOnes - k
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
+ if (numOnes >= k) {
+ return k;
+ }
+ k -= numOnes;
+ if (numZeros >= k) {
+ return numOnes;
+ }
+ k -= numZeros;
+ return numOnes - k;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
+ if (numOnes >= k) {
+ return k;
+ }
+ k -= numOnes;
+ if (numZeros >= k) {
+ return numOnes;
+ }
+ k -= numZeros;
+ return numOnes - k;
+ }
+};
+```
+
+### **Go**
+
+```go
+func kItemsWithMaximumSum(numOnes int, numZeros int, numNegOnes int, k int) int {
+ if numOnes >= k {
+ return k
+ }
+ k -= numOnes
+ if numZeros >= k {
+ return numOnes
+ }
+ k -= numZeros
+ return numOnes - k
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md b/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md
new file mode 100644
index 0000000000000..96fa6638bf90a
--- /dev/null
+++ b/solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md
@@ -0,0 +1,125 @@
+# [2600. K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum)
+
+[中文文档](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README.md)
+
+## Description
+
+There is a bag that consists of items, each item has a number 1
, 0
, or -1
written on it.
+
+You are given four non-negative integers numOnes
, numZeros
, numNegOnes
, and k
.
+
+The bag initially contains:
+
+
+ numOnes
items with 1
s written on them.
+ numZeroes
items with 0
s written on them.
+ numNegOnes
items with -1
s written on them.
+
+
+We want to pick exactly k
items among the available items. Return the maximum possible sum of numbers written on the items.
+
+
+Example 1:
+
+
+Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
+Output: 2
+Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
+It can be proven that 2 is the maximum possible sum.
+
+
+Example 2:
+
+
+Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
+Output: 3
+Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
+It can be proven that 3 is the maximum possible sum.
+
+
+
+Constraints:
+
+
+ 0 <= numOnes, numZeros, numNegOnes <= 50
+ 0 <= k <= numOnes + numZeros + numNegOnes
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:
+ if numOnes >= k:
+ return k
+ k -= numOnes
+ if numZeros >= k:
+ return numOnes
+ k -= numZeros
+ return numOnes - k
+```
+
+### **Java**
+
+```java
+class Solution {
+ public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
+ if (numOnes >= k) {
+ return k;
+ }
+ k -= numOnes;
+ if (numZeros >= k) {
+ return numOnes;
+ }
+ k -= numZeros;
+ return numOnes - k;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
+ if (numOnes >= k) {
+ return k;
+ }
+ k -= numOnes;
+ if (numZeros >= k) {
+ return numOnes;
+ }
+ k -= numZeros;
+ return numOnes - k;
+ }
+};
+```
+
+### **Go**
+
+```go
+func kItemsWithMaximumSum(numOnes int, numZeros int, numNegOnes int, k int) int {
+ if numOnes >= k {
+ return k
+ }
+ k -= numOnes
+ if numZeros >= k {
+ return numOnes
+ }
+ k -= numZeros
+ return numOnes - k
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.cpp b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.cpp
new file mode 100644
index 0000000000000..1a0a7c8fa7ca4
--- /dev/null
+++ b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.cpp
@@ -0,0 +1,14 @@
+class Solution {
+public:
+ int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
+ if (numOnes >= k) {
+ return k;
+ }
+ k -= numOnes;
+ if (numZeros >= k) {
+ return numOnes;
+ }
+ k -= numZeros;
+ return numOnes - k;
+ }
+};
\ No newline at end of file
diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.go b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.go
new file mode 100644
index 0000000000000..67194021d1202
--- /dev/null
+++ b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.go
@@ -0,0 +1,11 @@
+func kItemsWithMaximumSum(numOnes int, numZeros int, numNegOnes int, k int) int {
+ if numOnes >= k {
+ return k
+ }
+ k -= numOnes
+ if numZeros >= k {
+ return numOnes
+ }
+ k -= numZeros
+ return numOnes - k
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.java b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.java
new file mode 100644
index 0000000000000..d23f916602055
--- /dev/null
+++ b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.java
@@ -0,0 +1,13 @@
+class Solution {
+ public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
+ if (numOnes >= k) {
+ return k;
+ }
+ k -= numOnes;
+ if (numZeros >= k) {
+ return numOnes;
+ }
+ k -= numZeros;
+ return numOnes - k;
+ }
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.py b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.py
new file mode 100644
index 0000000000000..7916c3224884a
--- /dev/null
+++ b/solution/2600-2699/2600.K Items With the Maximum Sum/Solution.py
@@ -0,0 +1,9 @@
+class Solution:
+ def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:
+ if numOnes >= k:
+ return k
+ k -= numOnes
+ if numZeros >= k:
+ return numOnes
+ k -= numZeros
+ return numOnes - k
diff --git a/solution/2600-2699/2601.Prime Subtraction Operation/README.md b/solution/2600-2699/2601.Prime Subtraction Operation/README.md
new file mode 100644
index 0000000000000..2ee275fc91f95
--- /dev/null
+++ b/solution/2600-2699/2601.Prime Subtraction Operation/README.md
@@ -0,0 +1,220 @@
+# [2601. 质数减法运算](https://leetcode.cn/problems/prime-subtraction-operation)
+
+[English Version](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个下标从 0 开始的整数数组 nums
,数组长度为 n
。
+
+你可以执行无限次下述运算:
+
+
+ - 选择一个之前未选过的下标
i
,并选择一个 严格小于 nums[i]
的质数 p
,从 nums[i]
中减去 p
。
+
+
+如果你能通过上述运算使得 nums
成为严格递增数组,则返回 true
;否则返回 false
。
+
+严格递增数组 中的每个元素都严格大于其前面的元素。
+
+
+
+示例 1:
+
+
+输入:nums = [4,9,6,10]
+输出:true
+解释:
+在第一次运算中:选择 i = 0 和 p = 3 ,然后从 nums[0] 减去 3 ,nums 变为 [1,9,6,10] 。
+在第二次运算中:选择 i = 1 和 p = 7 ,然后从 nums[1] 减去 7 ,nums 变为 [1,2,6,10] 。
+第二次运算后,nums 按严格递增顺序排序,因此答案为 true 。
+
+示例 2:
+
+
+输入:nums = [6,8,11,12]
+输出:true
+解释:nums 从一开始就按严格递增顺序排序,因此不需要执行任何运算。
+
+示例 3:
+
+
+输入:nums = [5,8,3]
+输出:false
+解释:可以证明,执行运算无法使 nums 按严格递增顺序排序,因此答案是 false 。
+
+
+
+提示:
+
+
+ 1 <= nums.length <= 1000
+ 1 <= nums[i] <= 1000
+ nums.length == n
+
+
+## 解法
+
+
+
+**方法一:预处理质数 + 二分查找**
+
+我们先预处理得到 $1000$ 以内的所有质数,记录在数组 $p$ 中。
+
+对于数组 $nums$ 中的每个元素 $nums[i]$,我们需要找到一个质数 $p[j]$,使得 p[j] \gt nums[i] - nums[i + 1],并且 $p[j]$ 尽可能小。如果找不到这样的质数,说明无法通过减法运算使得 $nums$ 严格递增,返回 `false`。如果找到了这样的质数,我们就将 $nums[i]$ 减去 $p[j]$,并继续处理下一个元素。
+
+如果 $nums$ 中的所有元素都处理完了,说明可以通过减法运算使得 $nums$ 严格递增,返回 `true`。
+
+时间复杂度 $O(n \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def primeSubOperation(self, nums: List[int]) -> bool:
+ p = []
+ for i in range(2, max(nums)):
+ for j in p:
+ if i % j == 0:
+ break
+ else:
+ p.append(i)
+
+ n = len(nums)
+ for i in range(n - 2, -1, -1):
+ if nums[i] < nums[i + 1]:
+ continue
+ j = bisect_right(p, nums[i] - nums[i + 1])
+ if j == len(p) or p[j] >= nums[i]:
+ return False
+ nums[i] -= p[j]
+ return True
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public boolean primeSubOperation(int[] nums) {
+ List p = new ArrayList<>();
+ for (int i = 2; i <= 1000; ++i) {
+ boolean ok = true;
+ for (int j : p) {
+ if (i % j == 0) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ p.add(i);
+ }
+ }
+ int n = nums.length;
+ for (int i = n - 2; i >= 0; --i) {
+ if (nums[i] < nums[i + 1]) {
+ continue;
+ }
+ int j = search(p, nums[i] - nums[i + 1]);
+ if (j == p.size() || p.get(j) >= nums[i]) {
+ return false;
+ }
+ nums[i] -= p.get(j);
+ }
+ return true;
+ }
+
+ private int search(List nums, int x) {
+ int l = 0, r = nums.size();
+ while (l < r) {
+ int mid = (l + r) >> 1;
+ if (nums.get(mid) > x) {
+ r = mid;
+ } else {
+ l = mid + 1;
+ }
+ }
+ return l;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ bool primeSubOperation(vector& nums) {
+ vector p;
+ for (int i = 2; i <= 1000; ++i) {
+ bool ok = true;
+ for (int j : p) {
+ if (i % j == 0) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ p.push_back(i);
+ }
+ }
+ int n = nums.size();
+ for (int i = n - 2; i >= 0; --i) {
+ if (nums[i] < nums[i + 1]) {
+ continue;
+ }
+ int j = upper_bound(p.begin(), p.end(), nums[i] - nums[i + 1]) - p.begin();
+ if (j == p.size() || p[j] >= nums[i]) {
+ return false;
+ }
+ nums[i] -= p[j];
+ }
+ return true;
+ }
+};
+```
+
+### **Go**
+
+```go
+func primeSubOperation(nums []int) bool {
+ p := []int{}
+ for i := 2; i <= 1000; i++ {
+ ok := true
+ for _, j := range p {
+ if i%j == 0 {
+ ok = false
+ break
+ }
+ }
+ if ok {
+ p = append(p, i)
+ }
+ }
+ for i := len(nums) - 2; i >= 0; i-- {
+ if nums[i] < nums[i+1] {
+ continue
+ }
+ j := sort.SearchInts(p, nums[i]-nums[i+1]+1)
+ if j == len(p) || p[j] >= nums[i] {
+ return false
+ }
+ nums[i] -= p[j]
+ }
+ return true
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2601.Prime Subtraction Operation/README_EN.md b/solution/2600-2699/2601.Prime Subtraction Operation/README_EN.md
new file mode 100644
index 0000000000000..647b236c06b5f
--- /dev/null
+++ b/solution/2600-2699/2601.Prime Subtraction Operation/README_EN.md
@@ -0,0 +1,199 @@
+# [2601. Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation)
+
+[中文文档](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README.md)
+
+## Description
+
+You are given a 0-indexed integer array nums
of length n
.
+
+You can perform the following operation as many times as you want:
+
+
+ - Pick an index
i
that you haven’t picked before, and pick a prime p
strictly less than nums[i]
, then subtract p
from nums[i]
.
+
+
+Return true if you can make nums
a strictly increasing array using the above operation and false otherwise.
+
+A strictly increasing array is an array whose each element is strictly greater than its preceding element.
+
+
+Example 1:
+
+
+Input: nums = [4,9,6,10]
+Output: true
+Explanation: In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
+In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
+After the second operation, nums is sorted in strictly increasing order, so the answer is true.
+
+Example 2:
+
+
+Input: nums = [6,8,11,12]
+Output: true
+Explanation: Initially nums is sorted in strictly increasing order, so we don't need to make any operations.
+
+Example 3:
+
+
+Input: nums = [5,8,3]
+Output: false
+Explanation: It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.
+
+
+Constraints:
+
+
+ 1 <= nums.length <= 1000
+ 1 <= nums[i] <= 1000
+ nums.length == n
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def primeSubOperation(self, nums: List[int]) -> bool:
+ p = []
+ for i in range(2, max(nums)):
+ for j in p:
+ if i % j == 0:
+ break
+ else:
+ p.append(i)
+
+ n = len(nums)
+ for i in range(n - 2, -1, -1):
+ if nums[i] < nums[i + 1]:
+ continue
+ j = bisect_right(p, nums[i] - nums[i + 1])
+ if j == len(p) or p[j] >= nums[i]:
+ return False
+ nums[i] -= p[j]
+ return True
+```
+
+### **Java**
+
+```java
+class Solution {
+ public boolean primeSubOperation(int[] nums) {
+ List p = new ArrayList<>();
+ for (int i = 2; i <= 1000; ++i) {
+ boolean ok = true;
+ for (int j : p) {
+ if (i % j == 0) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ p.add(i);
+ }
+ }
+ int n = nums.length;
+ for (int i = n - 2; i >= 0; --i) {
+ if (nums[i] < nums[i + 1]) {
+ continue;
+ }
+ int j = search(p, nums[i] - nums[i + 1]);
+ if (j == p.size() || p.get(j) >= nums[i]) {
+ return false;
+ }
+ nums[i] -= p.get(j);
+ }
+ return true;
+ }
+
+ private int search(List nums, int x) {
+ int l = 0, r = nums.size();
+ while (l < r) {
+ int mid = (l + r) >> 1;
+ if (nums.get(mid) > x) {
+ r = mid;
+ } else {
+ l = mid + 1;
+ }
+ }
+ return l;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ bool primeSubOperation(vector& nums) {
+ vector p;
+ for (int i = 2; i <= 1000; ++i) {
+ bool ok = true;
+ for (int j : p) {
+ if (i % j == 0) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ p.push_back(i);
+ }
+ }
+ int n = nums.size();
+ for (int i = n - 2; i >= 0; --i) {
+ if (nums[i] < nums[i + 1]) {
+ continue;
+ }
+ int j = upper_bound(p.begin(), p.end(), nums[i] - nums[i + 1]) - p.begin();
+ if (j == p.size() || p[j] >= nums[i]) {
+ return false;
+ }
+ nums[i] -= p[j];
+ }
+ return true;
+ }
+};
+```
+
+### **Go**
+
+```go
+func primeSubOperation(nums []int) bool {
+ p := []int{}
+ for i := 2; i <= 1000; i++ {
+ ok := true
+ for _, j := range p {
+ if i%j == 0 {
+ ok = false
+ break
+ }
+ }
+ if ok {
+ p = append(p, i)
+ }
+ }
+ for i := len(nums) - 2; i >= 0; i-- {
+ if nums[i] < nums[i+1] {
+ continue
+ }
+ j := sort.SearchInts(p, nums[i]-nums[i+1]+1)
+ if j == len(p) || p[j] >= nums[i] {
+ return false
+ }
+ nums[i] -= p[j]
+ }
+ return true
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2601.Prime Subtraction Operation/Solution.cpp b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.cpp
new file mode 100644
index 0000000000000..efc35287301be
--- /dev/null
+++ b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.cpp
@@ -0,0 +1,30 @@
+class Solution {
+public:
+ bool primeSubOperation(vector& nums) {
+ vector p;
+ for (int i = 2; i <= 1000; ++i) {
+ bool ok = true;
+ for (int j : p) {
+ if (i % j == 0) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ p.push_back(i);
+ }
+ }
+ int n = nums.size();
+ for (int i = n - 2; i >= 0; --i) {
+ if (nums[i] < nums[i + 1]) {
+ continue;
+ }
+ int j = upper_bound(p.begin(), p.end(), nums[i] - nums[i + 1]) - p.begin();
+ if (j == p.size() || p[j] >= nums[i]) {
+ return false;
+ }
+ nums[i] -= p[j];
+ }
+ return true;
+ }
+};
\ No newline at end of file
diff --git a/solution/2600-2699/2601.Prime Subtraction Operation/Solution.go b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.go
new file mode 100644
index 0000000000000..3533bb6a797f6
--- /dev/null
+++ b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.go
@@ -0,0 +1,26 @@
+func primeSubOperation(nums []int) bool {
+ p := []int{}
+ for i := 2; i <= 1000; i++ {
+ ok := true
+ for _, j := range p {
+ if i%j == 0 {
+ ok = false
+ break
+ }
+ }
+ if ok {
+ p = append(p, i)
+ }
+ }
+ for i := len(nums) - 2; i >= 0; i-- {
+ if nums[i] < nums[i+1] {
+ continue
+ }
+ j := sort.SearchInts(p, nums[i]-nums[i+1]+1)
+ if j == len(p) || p[j] >= nums[i] {
+ return false
+ }
+ nums[i] -= p[j]
+ }
+ return true
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2601.Prime Subtraction Operation/Solution.java b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.java
new file mode 100644
index 0000000000000..cf5f2dcdd2abf
--- /dev/null
+++ b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.java
@@ -0,0 +1,42 @@
+class Solution {
+ public boolean primeSubOperation(int[] nums) {
+ List p = new ArrayList<>();
+ for (int i = 2; i <= 1000; ++i) {
+ boolean ok = true;
+ for (int j : p) {
+ if (i % j == 0) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ p.add(i);
+ }
+ }
+ int n = nums.length;
+ for (int i = n - 2; i >= 0; --i) {
+ if (nums[i] < nums[i + 1]) {
+ continue;
+ }
+ int j = search(p, nums[i] - nums[i + 1]);
+ if (j == p.size() || p.get(j) >= nums[i]) {
+ return false;
+ }
+ nums[i] -= p.get(j);
+ }
+ return true;
+ }
+
+ private int search(List nums, int x) {
+ int l = 0, r = nums.size();
+ while (l < r) {
+ int mid = (l + r) >> 1;
+ if (nums.get(mid) > x) {
+ r = mid;
+ } else {
+ l = mid + 1;
+ }
+ }
+ return l;
+ }
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2601.Prime Subtraction Operation/Solution.py b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.py
new file mode 100644
index 0000000000000..7490c6969936e
--- /dev/null
+++ b/solution/2600-2699/2601.Prime Subtraction Operation/Solution.py
@@ -0,0 +1,19 @@
+class Solution:
+ def primeSubOperation(self, nums: List[int]) -> bool:
+ p = []
+ for i in range(2, max(nums)):
+ for j in p:
+ if i % j == 0:
+ break
+ else:
+ p.append(i)
+
+ n = len(nums)
+ for i in range(n - 2, -1, -1):
+ if nums[i] < nums[i + 1]:
+ continue
+ j = bisect_right(p, nums[i] - nums[i + 1])
+ if j == len(p) or p[j] >= nums[i]:
+ return False
+ nums[i] -= p[j]
+ return True
diff --git a/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README.md b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README.md
new file mode 100644
index 0000000000000..814c702a41bbc
--- /dev/null
+++ b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README.md
@@ -0,0 +1,188 @@
+# [2602. 使数组元素全部相等的最少操作次数](https://leetcode.cn/problems/minimum-operations-to-make-all-array-elements-equal)
+
+[English Version](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个正整数数组 nums
。
+
+同时给你一个长度为 m
的整数数组 queries
。第 i
个查询中,你需要将 nums
中所有元素变成 queries[i]
。你可以执行以下操作 任意 次:
+
+
+ - 将数组里一个元素 增大 或者 减小
1
。
+
+
+请你返回一个长度为 m
的数组 answer
,其中 answer[i]
是将 nums
中所有元素变成 queries[i]
的 最少 操作次数。
+
+注意,每次查询后,数组变回最开始的值。
+
+
+
+示例 1:
+
+输入:nums = [3,1,6,8], queries = [1,5]
+输出:[14,10]
+解释:第一个查询,我们可以执行以下操作:
+- 将 nums[0] 减小 2 次,nums = [1,1,6,8] 。
+- 将 nums[2] 减小 5 次,nums = [1,1,1,8] 。
+- 将 nums[3] 减小 7 次,nums = [1,1,1,1] 。
+第一个查询的总操作次数为 2 + 5 + 7 = 14 。
+第二个查询,我们可以执行以下操作:
+- 将 nums[0] 增大 2 次,nums = [5,1,6,8] 。
+- 将 nums[1] 增大 4 次,nums = [5,5,6,8] 。
+- 将 nums[2] 减小 1 次,nums = [5,5,5,8] 。
+- 将 nums[3] 减小 3 次,nums = [5,5,5,5] 。
+第二个查询的总操作次数为 2 + 4 + 1 + 3 = 10 。
+
+
+示例 2:
+
+输入:nums = [2,9,6,3], queries = [10]
+输出:[20]
+解释:我们可以将数组中所有元素都增大到 10 ,总操作次数为 8 + 1 + 4 + 7 = 20 。
+
+
+
+
+提示:
+
+
+ n == nums.length
+ m == queries.length
+ 1 <= n, m <= 105
+ 1 <= nums[i], queries[i] <= 109
+
+
+## 解法
+
+
+
+**方法一:排序 + 前缀和 + 二分查找**
+
+我们先将数组 $nums$ 进行排序,并计算出长度为 $n+1$ 的前缀和数组 $s$,其中 $s[i]$ 表示数组 $nums$ 中前 $i$ 个元素的和。
+
+接下来,遍历每个查询 $queries[i]$,我们需要将所有大于 $queries[i]$ 的元素减小到 $queries[i]$,将所有小于 $queries[i]$ 的元素增大到 $queries[i]$。
+
+我们可以通过二分查找找到数组 $nums$ 中第一个大于 $queries[i]$ 的元素的下标 $i$,则有 $n-i$ 个元素需要减小到 $queries[i]$,这些元素的和为 $s[n]-s[i]$,这些元素的和需要减去 $n-i$ 个 $queries[i]$,因此,这些元素减小到 $queries[i]$ 的总操作次数为 $s[n]-s[i]-(n-i)\times queries[i]$。
+
+同理,我们可以找到数组 $nums$ 中第一个大于等于 $queries[i]$ 的元素的下标 $i$,则有 $i$ 个元素需要增大到 $queries[i]$,这些元素的和为 $s[i]$,因此,这些元素增大到 $queries[i]$ 的总操作次数为 $queries[i]\times i-s[i]$。
+
+最后,将这两个总操作次数相加,即为将数组 $nums$ 中所有元素变成 $queries[i]$ 的最少操作次数,即 $ans[i]=s[n]-s[i]-(n-i)\times queries[i]+queries[i]\times i-s[i]$。
+
+时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:
+ nums.sort()
+ s = list(accumulate(nums, initial=0))
+ ans = []
+ for x in queries:
+ i = bisect_left(nums, x + 1)
+ t = s[-1] - s[i] - (len(nums) - i) * x
+ i = bisect_left(nums, x)
+ t += x * i - s[i]
+ ans.append(t)
+ return ans
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public List minOperations(int[] nums, int[] queries) {
+ Arrays.sort(nums);
+ int n = nums.length;
+ long[] s = new long[n + 1];
+ for (int i = 0; i < n; ++i) {
+ s[i + 1] = s[i] + nums[i];
+ }
+ List ans = new ArrayList<>();
+ for (int x : queries) {
+ int i = search(nums, x + 1);
+ long t = s[n] - s[i] - 1L * (n - i) * x;
+ i = search(nums, x);
+ t += 1L * x * i - s[i];
+ ans.add(t);
+ }
+ return ans;
+ }
+
+ private int search(int[] nums, int x) {
+ int l = 0, r = nums.length;
+ while (l < r) {
+ int mid = (l + r) >> 1;
+ if (nums[mid] >= x) {
+ r = mid;
+ } else {
+ l = mid + 1;
+ }
+ }
+ return l;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ vector minOperations(vector& nums, vector& queries) {
+ sort(nums.begin(), nums.end());
+ int n = nums.size();
+ vector s(n + 1);
+ for (int i = 0; i < n; ++i) {
+ s[i + 1] = s[i] + nums[i];
+ }
+ vector ans;
+ for (auto& x : queries) {
+ int i = lower_bound(nums.begin(), nums.end(), x + 1) - nums.begin();
+ long long t = s[n] - s[i] - 1LL * (n - i) * x;
+ i = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
+ t += 1LL * x * i - s[i];
+ ans.push_back(t);
+ }
+ return ans;
+ }
+};
+```
+
+### **Go**
+
+```go
+func minOperations(nums []int, queries []int) (ans []int64) {
+ sort.Ints(nums)
+ n := len(nums)
+ s := make([]int, n+1)
+ for i, x := range nums {
+ s[i+1] = s[i] + x
+ }
+ for _, x := range queries {
+ i := sort.SearchInts(nums, x+1)
+ t := s[n] - s[i] - (n-i)*x
+ i = sort.SearchInts(nums, x)
+ t += x*i - s[i]
+ ans = append(ans, int64(t))
+ }
+ return
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README_EN.md b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README_EN.md
new file mode 100644
index 0000000000000..ea7a514489f5f
--- /dev/null
+++ b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README_EN.md
@@ -0,0 +1,166 @@
+# [2602. Minimum Operations to Make All Array Elements Equal](https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal)
+
+[中文文档](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README.md)
+
+## Description
+
+You are given an array nums
consisting of positive integers.
+
+You are also given an integer array queries
of size m
. For the ith
query, you want to make all of the elements of nums
equal to queries[i]
. You can perform the following operation on the array any number of times:
+
+
+ - Increase or decrease an element of the array by
1
.
+
+
+Return an array answer
of size m
where answer[i]
is the minimum number of operations to make all elements of nums
equal to queries[i]
.
+
+Note that after each query the array is reset to its original state.
+
+
+Example 1:
+
+
+Input: nums = [3,1,6,8], queries = [1,5]
+Output: [14,10]
+Explanation: For the first query we can do the following operations:
+- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
+- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
+- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
+So the total number of operations for the first query is 2 + 5 + 7 = 14.
+For the second query we can do the following operations:
+- Increase nums[0] 2 times, so that nums = [5,1,6,8].
+- Increase nums[1] 4 times, so that nums = [5,5,6,8].
+- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
+- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
+So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
+
+
+Example 2:
+
+
+Input: nums = [2,9,6,3], queries = [10]
+Output: [20]
+Explanation: We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
+
+
+
+Constraints:
+
+
+ n == nums.length
+ m == queries.length
+ 1 <= n, m <= 105
+ 1 <= nums[i], queries[i] <= 109
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:
+ nums.sort()
+ s = list(accumulate(nums, initial=0))
+ ans = []
+ for x in queries:
+ i = bisect_left(nums, x + 1)
+ t = s[-1] - s[i] - (len(nums) - i) * x
+ i = bisect_left(nums, x)
+ t += x * i - s[i]
+ ans.append(t)
+ return ans
+```
+
+### **Java**
+
+```java
+class Solution {
+ public List minOperations(int[] nums, int[] queries) {
+ Arrays.sort(nums);
+ int n = nums.length;
+ long[] s = new long[n + 1];
+ for (int i = 0; i < n; ++i) {
+ s[i + 1] = s[i] + nums[i];
+ }
+ List ans = new ArrayList<>();
+ for (int x : queries) {
+ int i = search(nums, x + 1);
+ long t = s[n] - s[i] - 1L * (n - i) * x;
+ i = search(nums, x);
+ t += 1L * x * i - s[i];
+ ans.add(t);
+ }
+ return ans;
+ }
+
+ private int search(int[] nums, int x) {
+ int l = 0, r = nums.length;
+ while (l < r) {
+ int mid = (l + r) >> 1;
+ if (nums[mid] >= x) {
+ r = mid;
+ } else {
+ l = mid + 1;
+ }
+ }
+ return l;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ vector minOperations(vector& nums, vector& queries) {
+ sort(nums.begin(), nums.end());
+ int n = nums.size();
+ vector s(n + 1);
+ for (int i = 0; i < n; ++i) {
+ s[i + 1] = s[i] + nums[i];
+ }
+ vector ans;
+ for (auto& x : queries) {
+ int i = lower_bound(nums.begin(), nums.end(), x + 1) - nums.begin();
+ long long t = s[n] - s[i] - 1LL * (n - i) * x;
+ i = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
+ t += 1LL * x * i - s[i];
+ ans.push_back(t);
+ }
+ return ans;
+ }
+};
+```
+
+### **Go**
+
+```go
+func minOperations(nums []int, queries []int) (ans []int64) {
+ sort.Ints(nums)
+ n := len(nums)
+ s := make([]int, n+1)
+ for i, x := range nums {
+ s[i+1] = s[i] + x
+ }
+ for _, x := range queries {
+ i := sort.SearchInts(nums, x+1)
+ t := s[n] - s[i] - (n-i)*x
+ i = sort.SearchInts(nums, x)
+ t += x*i - s[i]
+ ans = append(ans, int64(t))
+ }
+ return
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.cpp b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.cpp
new file mode 100644
index 0000000000000..8e03db5b7ab11
--- /dev/null
+++ b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.cpp
@@ -0,0 +1,20 @@
+class Solution {
+public:
+ vector minOperations(vector& nums, vector& queries) {
+ sort(nums.begin(), nums.end());
+ int n = nums.size();
+ vector s(n + 1);
+ for (int i = 0; i < n; ++i) {
+ s[i + 1] = s[i] + nums[i];
+ }
+ vector ans;
+ for (auto& x : queries) {
+ int i = lower_bound(nums.begin(), nums.end(), x + 1) - nums.begin();
+ long long t = s[n] - s[i] - 1LL * (n - i) * x;
+ i = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
+ t += 1LL * x * i - s[i];
+ ans.push_back(t);
+ }
+ return ans;
+ }
+};
\ No newline at end of file
diff --git a/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.go b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.go
new file mode 100644
index 0000000000000..a9d4a73a24393
--- /dev/null
+++ b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.go
@@ -0,0 +1,16 @@
+func minOperations(nums []int, queries []int) (ans []int64) {
+ sort.Ints(nums)
+ n := len(nums)
+ s := make([]int, n+1)
+ for i, x := range nums {
+ s[i+1] = s[i] + x
+ }
+ for _, x := range queries {
+ i := sort.SearchInts(nums, x+1)
+ t := s[n] - s[i] - (n-i)*x
+ i = sort.SearchInts(nums, x)
+ t += x*i - s[i]
+ ans = append(ans, int64(t))
+ }
+ return
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.java b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.java
new file mode 100644
index 0000000000000..a56d69ae6990e
--- /dev/null
+++ b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.java
@@ -0,0 +1,32 @@
+class Solution {
+ public List minOperations(int[] nums, int[] queries) {
+ Arrays.sort(nums);
+ int n = nums.length;
+ long[] s = new long[n + 1];
+ for (int i = 0; i < n; ++i) {
+ s[i + 1] = s[i] + nums[i];
+ }
+ List ans = new ArrayList<>();
+ for (int x : queries) {
+ int i = search(nums, x + 1);
+ long t = s[n] - s[i] - 1L * (n - i) * x;
+ i = search(nums, x);
+ t += 1L * x * i - s[i];
+ ans.add(t);
+ }
+ return ans;
+ }
+
+ private int search(int[] nums, int x) {
+ int l = 0, r = nums.length;
+ while (l < r) {
+ int mid = (l + r) >> 1;
+ if (nums[mid] >= x) {
+ r = mid;
+ } else {
+ l = mid + 1;
+ }
+ }
+ return l;
+ }
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.py b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.py
new file mode 100644
index 0000000000000..d0148dde02434
--- /dev/null
+++ b/solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.py
@@ -0,0 +1,12 @@
+class Solution:
+ def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:
+ nums.sort()
+ s = list(accumulate(nums, initial=0))
+ ans = []
+ for x in queries:
+ i = bisect_left(nums, x + 1)
+ t = s[-1] - s[i] - (len(nums) - i) * x
+ i = bisect_left(nums, x)
+ t += x * i - s[i]
+ ans.append(t)
+ return ans
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/README.md b/solution/2600-2699/2603.Collect Coins in a Tree/README.md
new file mode 100644
index 0000000000000..784ff5236fc29
--- /dev/null
+++ b/solution/2600-2699/2603.Collect Coins in a Tree/README.md
@@ -0,0 +1,281 @@
+# [2603. 收集树中金币](https://leetcode.cn/problems/collect-coins-in-a-tree)
+
+[English Version](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个 n
个节点的无向无根树,节点编号从 0
到 n - 1
。给你整数 n
和一个长度为 n - 1
的二维整数数组 edges
,其中 edges[i] = [ai, bi]
表示树中节点 ai
和 bi
之间有一条边。再给你一个长度为 n
的数组 coins
,其中 coins[i]
可能为 0
也可能为 1
,1
表示节点 i
处有一个金币。
+
+一开始,你需要选择树中任意一个节点出发。你可以执行下述操作任意次:
+
+
+ - 收集距离当前节点距离为
2
以内的所有金币,或者
+ - 移动到树中一个相邻节点。
+
+
+你需要收集树中所有的金币,并且回到出发节点,请你返回最少经过的边数。
+
+如果你多次经过一条边,每一次经过都会给答案加一。
+
+
+
+示例 1:
+
+
+
+输入:coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]
+输出:2
+解释:从节点 2 出发,收集节点 0 处的金币,移动到节点 3 ,收集节点 5 处的金币,然后移动回节点 2 。
+
+
+示例 2:
+
+
+
+输入:coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]
+输出:2
+解释:从节点 0 出发,收集节点 4 和 3 处的金币,移动到节点 2 处,收集节点 7 处的金币,移动回节点 0 。
+
+
+
+
+提示:
+
+
+ n == coins.length
+ 1 <= n <= 3 * 104
+ 0 <= coins[i] <= 1
+ edges.length == n - 1
+ edges[i].length == 2
+ 0 <= ai, bi < n
+ ai != bi
+ edges
表示一棵合法的树。
+
+
+## 解法
+
+
+
+**方法一:拓扑排序**
+
+我们先将 $edges$ 中的边转换成邻接表 $g$,其中 $g[i]$ 表示节点 $i$ 的所有邻接节点,用集合表示。
+
+接下来我们遍历所有节点,找到 $coins[i]=0$ 且 $g[i]$ 中只有一个节点的节点(也即是金币为 $0$ 的叶子节点),将其加入队列 $q$ 中。
+
+然后我们不断地从队列中取出节点,将其从邻接表中删除,然后判断其邻接节点是否满足 $coins[j]=0$ 且 $g[j]$ 中只有一个节点的条件,如果满足则将其加入队列 $q$ 中。循环,直至队列为空。
+
+经过上述操作后,我们得到了一棵新的树,且树的叶子节点都是金币为 $1$ 的节点。
+
+然后,我们在删除剩下的两层叶子节点,最终得到的是一棵所有节点都需要被访问的节点,我们只需要统计其边数,乘上 $2$,即为答案。
+
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点数。
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def collectTheCoins(self, coins: List[int], edges: List[List[int]]) -> int:
+ g = defaultdict(set)
+ for a, b in edges:
+ g[a].add(b)
+ g[b].add(a)
+ n = len(coins)
+ q = deque(i for i in range(n) if len(g[i]) == 1 and coins[i] == 0)
+ while q:
+ i = q.popleft()
+ for j in g[i]:
+ g[j].remove(i)
+ if coins[j] == 0 and len(g[j]) == 1:
+ q.append(j)
+ g[i].clear()
+ for k in range(2):
+ q = [i for i in range(n) if len(g[i]) == 1]
+ for i in q:
+ for j in g[i]:
+ g[j].remove(i)
+ g[i].clear()
+ return sum(len(g[a]) > 0 and len(g[b]) > 0 for a, b in edges) * 2
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public int collectTheCoins(int[] coins, int[][] edges) {
+ int n = coins.length;
+ Set[] g = new Set[n];
+ Arrays.setAll(g, k -> new HashSet<>());
+ for (var e : edges) {
+ int a = e[0], b = e[1];
+ g[a].add(b);
+ g[b].add(a);
+ }
+ Deque q = new ArrayDeque<>();
+ for (int i = 0; i < n; ++i) {
+ if (coins[i] == 0 && g[i].size() == 1) {
+ q.offer(i);
+ }
+ }
+ while (!q.isEmpty()) {
+ int i = q.poll();
+ for (int j : g[i]) {
+ g[j].remove(i);
+ if (coins[j] == 0 && g[j].size() == 1) {
+ q.offer(j);
+ }
+ }
+ g[i].clear();
+ }
+ q.clear();
+ for (int k = 0; k < 2; ++k) {
+ for (int i = 0; i < n; ++i) {
+ if (g[i].size() == 1) {
+ q.offer(i);
+ }
+ }
+ for (int i : q) {
+ for (int j : g[i]) {
+ g[j].remove(i);
+ }
+ g[i].clear();
+ }
+ }
+ int ans = 0;
+ for (var e : edges) {
+ int a = e[0], b = e[1];
+ if (g[a].size() > 0 && g[b].size() > 0) {
+ ans += 2;
+ }
+ }
+ return ans;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int collectTheCoins(vector& coins, vector>& edges) {
+ int n = coins.size();
+ unordered_set g[n];
+ for (auto& e : edges) {
+ int a = e[0], b = e[1];
+ g[a].insert(b);
+ g[b].insert(a);
+ }
+ queue q;
+ for (int i = 0; i < n; ++i) {
+ if (coins[i] == 0 && g[i].size() == 1) {
+ q.push(i);
+ }
+ }
+ while (!q.empty()) {
+ int i = q.front();
+ q.pop();
+ for (int j : g[i]) {
+ g[j].erase(i);
+ if (coins[j] == 0 && g[j].size() == 1) {
+ q.push(j);
+ }
+ }
+ g[i].clear();
+ }
+ for (int k = 0; k < 2; ++k) {
+ vector q;
+ for (int i = 0; i < n; ++i) {
+ if (g[i].size() == 1) {
+ q.push_back(i);
+ }
+ }
+ for (int i : q) {
+ for (int j : g[i]) {
+ g[j].erase(i);
+ }
+ g[i].clear();
+ }
+ }
+ int ans = 0;
+ for (auto& e : edges) {
+ int a = e[0], b = e[1];
+ if (g[a].size() && g[b].size()) {
+ ans += 2;
+ }
+ }
+ return ans;
+ }
+};
+```
+
+### **Go**
+
+```go
+func collectTheCoins(coins []int, edges [][]int) int {
+ n := len(coins)
+ g := make([]map[int]bool, n)
+ for i := range g {
+ g[i] = map[int]bool{}
+ }
+ for _, e := range edges {
+ a, b := e[0], e[1]
+ g[a][b] = true
+ g[b][a] = true
+ }
+ q := []int{}
+ for i, c := range coins {
+ if c == 0 && len(g[i]) == 1 {
+ q = append(q, i)
+ }
+ }
+ for len(q) > 0 {
+ i := q[0]
+ q = q[1:]
+ for j := range g[i] {
+ delete(g[j], i)
+ if coins[j] == 0 && len(g[j]) == 1 {
+ q = append(q, j)
+ }
+ }
+ g[i] = map[int]bool{}
+ }
+ for k := 0; k < 2; k++ {
+ q := []int{}
+ for i := range coins {
+ if len(g[i]) == 1 {
+ q = append(q, i)
+ }
+ }
+ for _, i := range q {
+ for j := range g[i] {
+ delete(g[j], i)
+ }
+ g[i] = map[int]bool{}
+ }
+ }
+ ans := 0
+ for _, e := range edges {
+ a, b := e[0], e[1]
+ if len(g[a]) > 0 && len(g[b]) > 0 {
+ ans += 2
+ }
+ }
+ return ans
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/README_EN.md b/solution/2600-2699/2603.Collect Coins in a Tree/README_EN.md
new file mode 100644
index 0000000000000..be66e2c9afe1b
--- /dev/null
+++ b/solution/2600-2699/2603.Collect Coins in a Tree/README_EN.md
@@ -0,0 +1,255 @@
+# [2603. Collect Coins in a Tree](https://leetcode.com/problems/collect-coins-in-a-tree)
+
+[中文文档](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README.md)
+
+## Description
+
+There exists an undirected and unrooted tree with n
nodes indexed from 0
to n - 1
. You are given an 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 an array coins
of size n
where coins[i]
can be either 0
or 1
, where 1
indicates the presence of a coin in the vertex i
.
+
+Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times:
+
+
+ - Collect all the coins that are at a distance of at most
2
from the current vertex, or
+ - Move to any adjacent vertex in the tree.
+
+
+Find the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex.
+
+Note that if you pass an edge several times, you need to count it into the answer several times.
+
+
+Example 1:
+
+
+Input: coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]
+Output: 2
+Explanation: Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2.
+
+
+Example 2:
+
+
+Input: coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]
+Output: 2
+Explanation: Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2, collect the coin at vertex 7, then move back to vertex 0.
+
+
+
+Constraints:
+
+
+ n == coins.length
+ 1 <= n <= 3 * 104
+ 0 <= coins[i] <= 1
+ edges.length == n - 1
+ edges[i].length == 2
+ 0 <= ai, bi < n
+ ai != bi
+ edges
represents a valid tree.
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def collectTheCoins(self, coins: List[int], edges: List[List[int]]) -> int:
+ g = defaultdict(set)
+ for a, b in edges:
+ g[a].add(b)
+ g[b].add(a)
+ n = len(coins)
+ q = deque(i for i in range(n) if len(g[i]) == 1 and coins[i] == 0)
+ while q:
+ i = q.popleft()
+ for j in g[i]:
+ g[j].remove(i)
+ if coins[j] == 0 and len(g[j]) == 1:
+ q.append(j)
+ g[i].clear()
+ for k in range(2):
+ q = [i for i in range(n) if len(g[i]) == 1]
+ for i in q:
+ for j in g[i]:
+ g[j].remove(i)
+ g[i].clear()
+ return sum(len(g[a]) > 0 and len(g[b]) > 0 for a, b in edges) * 2
+```
+
+### **Java**
+
+```java
+class Solution {
+ public int collectTheCoins(int[] coins, int[][] edges) {
+ int n = coins.length;
+ Set[] g = new Set[n];
+ Arrays.setAll(g, k -> new HashSet<>());
+ for (var e : edges) {
+ int a = e[0], b = e[1];
+ g[a].add(b);
+ g[b].add(a);
+ }
+ Deque q = new ArrayDeque<>();
+ for (int i = 0; i < n; ++i) {
+ if (coins[i] == 0 && g[i].size() == 1) {
+ q.offer(i);
+ }
+ }
+ while (!q.isEmpty()) {
+ int i = q.poll();
+ for (int j : g[i]) {
+ g[j].remove(i);
+ if (coins[j] == 0 && g[j].size() == 1) {
+ q.offer(j);
+ }
+ }
+ g[i].clear();
+ }
+ q.clear();
+ for (int k = 0; k < 2; ++k) {
+ for (int i = 0; i < n; ++i) {
+ if (g[i].size() == 1) {
+ q.offer(i);
+ }
+ }
+ for (int i : q) {
+ for (int j : g[i]) {
+ g[j].remove(i);
+ }
+ g[i].clear();
+ }
+ }
+ int ans = 0;
+ for (var e : edges) {
+ int a = e[0], b = e[1];
+ if (g[a].size() > 0 && g[b].size() > 0) {
+ ans += 2;
+ }
+ }
+ return ans;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int collectTheCoins(vector& coins, vector>& edges) {
+ int n = coins.size();
+ unordered_set g[n];
+ for (auto& e : edges) {
+ int a = e[0], b = e[1];
+ g[a].insert(b);
+ g[b].insert(a);
+ }
+ queue q;
+ for (int i = 0; i < n; ++i) {
+ if (coins[i] == 0 && g[i].size() == 1) {
+ q.push(i);
+ }
+ }
+ while (!q.empty()) {
+ int i = q.front();
+ q.pop();
+ for (int j : g[i]) {
+ g[j].erase(i);
+ if (coins[j] == 0 && g[j].size() == 1) {
+ q.push(j);
+ }
+ }
+ g[i].clear();
+ }
+ for (int k = 0; k < 2; ++k) {
+ vector q;
+ for (int i = 0; i < n; ++i) {
+ if (g[i].size() == 1) {
+ q.push_back(i);
+ }
+ }
+ for (int i : q) {
+ for (int j : g[i]) {
+ g[j].erase(i);
+ }
+ g[i].clear();
+ }
+ }
+ int ans = 0;
+ for (auto& e : edges) {
+ int a = e[0], b = e[1];
+ if (g[a].size() && g[b].size()) {
+ ans += 2;
+ }
+ }
+ return ans;
+ }
+};
+```
+
+### **Go**
+
+```go
+func collectTheCoins(coins []int, edges [][]int) int {
+ n := len(coins)
+ g := make([]map[int]bool, n)
+ for i := range g {
+ g[i] = map[int]bool{}
+ }
+ for _, e := range edges {
+ a, b := e[0], e[1]
+ g[a][b] = true
+ g[b][a] = true
+ }
+ q := []int{}
+ for i, c := range coins {
+ if c == 0 && len(g[i]) == 1 {
+ q = append(q, i)
+ }
+ }
+ for len(q) > 0 {
+ i := q[0]
+ q = q[1:]
+ for j := range g[i] {
+ delete(g[j], i)
+ if coins[j] == 0 && len(g[j]) == 1 {
+ q = append(q, j)
+ }
+ }
+ g[i] = map[int]bool{}
+ }
+ for k := 0; k < 2; k++ {
+ q := []int{}
+ for i := range coins {
+ if len(g[i]) == 1 {
+ q = append(q, i)
+ }
+ }
+ for _, i := range q {
+ for j := range g[i] {
+ delete(g[j], i)
+ }
+ g[i] = map[int]bool{}
+ }
+ }
+ ans := 0
+ for _, e := range edges {
+ a, b := e[0], e[1]
+ if len(g[a]) > 0 && len(g[b]) > 0 {
+ ans += 2
+ }
+ }
+ return ans
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/Solution.cpp b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.cpp
new file mode 100644
index 0000000000000..584782fff30fc
--- /dev/null
+++ b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.cpp
@@ -0,0 +1,51 @@
+class Solution {
+public:
+ int collectTheCoins(vector& coins, vector>& edges) {
+ int n = coins.size();
+ unordered_set g[n];
+ for (auto& e : edges) {
+ int a = e[0], b = e[1];
+ g[a].insert(b);
+ g[b].insert(a);
+ }
+ queue q;
+ for (int i = 0; i < n; ++i) {
+ if (coins[i] == 0 && g[i].size() == 1) {
+ q.push(i);
+ }
+ }
+ while (!q.empty()) {
+ int i = q.front();
+ q.pop();
+ for (int j : g[i]) {
+ g[j].erase(i);
+ if (coins[j] == 0 && g[j].size() == 1) {
+ q.push(j);
+ }
+ }
+ g[i].clear();
+ }
+ for (int k = 0; k < 2; ++k) {
+ vector q;
+ for (int i = 0; i < n; ++i) {
+ if (g[i].size() == 1) {
+ q.push_back(i);
+ }
+ }
+ for (int i : q) {
+ for (int j : g[i]) {
+ g[j].erase(i);
+ }
+ g[i].clear();
+ }
+ }
+ int ans = 0;
+ for (auto& e : edges) {
+ int a = e[0], b = e[1];
+ if (g[a].size() && g[b].size()) {
+ ans += 2;
+ }
+ }
+ return ans;
+ }
+};
\ No newline at end of file
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/Solution.go b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.go
new file mode 100644
index 0000000000000..82cac4d2d6688
--- /dev/null
+++ b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.go
@@ -0,0 +1,51 @@
+func collectTheCoins(coins []int, edges [][]int) int {
+ n := len(coins)
+ g := make([]map[int]bool, n)
+ for i := range g {
+ g[i] = map[int]bool{}
+ }
+ for _, e := range edges {
+ a, b := e[0], e[1]
+ g[a][b] = true
+ g[b][a] = true
+ }
+ q := []int{}
+ for i, c := range coins {
+ if c == 0 && len(g[i]) == 1 {
+ q = append(q, i)
+ }
+ }
+ for len(q) > 0 {
+ i := q[0]
+ q = q[1:]
+ for j := range g[i] {
+ delete(g[j], i)
+ if coins[j] == 0 && len(g[j]) == 1 {
+ q = append(q, j)
+ }
+ }
+ g[i] = map[int]bool{}
+ }
+ for k := 0; k < 2; k++ {
+ q := []int{}
+ for i := range coins {
+ if len(g[i]) == 1 {
+ q = append(q, i)
+ }
+ }
+ for _, i := range q {
+ for j := range g[i] {
+ delete(g[j], i)
+ }
+ g[i] = map[int]bool{}
+ }
+ }
+ ans := 0
+ for _, e := range edges {
+ a, b := e[0], e[1]
+ if len(g[a]) > 0 && len(g[b]) > 0 {
+ ans += 2
+ }
+ }
+ return ans
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/Solution.java b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.java
new file mode 100644
index 0000000000000..631f41eba9bae
--- /dev/null
+++ b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.java
@@ -0,0 +1,50 @@
+class Solution {
+ public int collectTheCoins(int[] coins, int[][] edges) {
+ int n = coins.length;
+ Set[] g = new Set[n];
+ Arrays.setAll(g, k -> new HashSet<>());
+ for (var e : edges) {
+ int a = e[0], b = e[1];
+ g[a].add(b);
+ g[b].add(a);
+ }
+ Deque q = new ArrayDeque<>();
+ for (int i = 0; i < n; ++i) {
+ if (coins[i] == 0 && g[i].size() == 1) {
+ q.offer(i);
+ }
+ }
+ while (!q.isEmpty()) {
+ int i = q.poll();
+ for (int j : g[i]) {
+ g[j].remove(i);
+ if (coins[j] == 0 && g[j].size() == 1) {
+ q.offer(j);
+ }
+ }
+ g[i].clear();
+ }
+ q.clear();
+ for (int k = 0; k < 2; ++k) {
+ for (int i = 0; i < n; ++i) {
+ if (g[i].size() == 1) {
+ q.offer(i);
+ }
+ }
+ for (int i : q) {
+ for (int j : g[i]) {
+ g[j].remove(i);
+ }
+ g[i].clear();
+ }
+ }
+ int ans = 0;
+ for (var e : edges) {
+ int a = e[0], b = e[1];
+ if (g[a].size() > 0 && g[b].size() > 0) {
+ ans += 2;
+ }
+ }
+ return ans;
+ }
+}
\ No newline at end of file
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/Solution.py b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.py
new file mode 100644
index 0000000000000..693ab7dadef4b
--- /dev/null
+++ b/solution/2600-2699/2603.Collect Coins in a Tree/Solution.py
@@ -0,0 +1,22 @@
+class Solution:
+ def collectTheCoins(self, coins: List[int], edges: List[List[int]]) -> int:
+ g = defaultdict(set)
+ for a, b in edges:
+ g[a].add(b)
+ g[b].add(a)
+ n = len(coins)
+ q = deque(i for i in range(n) if len(g[i]) == 1 and coins[i] == 0)
+ while q:
+ i = q.popleft()
+ for j in g[i]:
+ g[j].remove(i)
+ if coins[j] == 0 and len(g[j]) == 1:
+ q.append(j)
+ g[i].clear()
+ for k in range(2):
+ q = [i for i in range(n) if len(g[i]) == 1]
+ for i in q:
+ for j in g[i]:
+ g[j].remove(i)
+ g[i].clear()
+ return sum(len(g[a]) > 0 and len(g[b]) > 0 for a, b in edges) * 2
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/images/graph-2.png b/solution/2600-2699/2603.Collect Coins in a Tree/images/graph-2.png
new file mode 100644
index 0000000000000..abb216183fd03
Binary files /dev/null and b/solution/2600-2699/2603.Collect Coins in a Tree/images/graph-2.png differ
diff --git a/solution/2600-2699/2603.Collect Coins in a Tree/images/graph-4.png b/solution/2600-2699/2603.Collect Coins in a Tree/images/graph-4.png
new file mode 100644
index 0000000000000..c9964a2f1d3a3
Binary files /dev/null and b/solution/2600-2699/2603.Collect Coins in a Tree/images/graph-4.png differ
diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md
index 2f04243dcc3e7..83727101bfa1c 100644
--- a/solution/CONTEST_README.md
+++ b/solution/CONTEST_README.md
@@ -11,8 +11,8 @@
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
-| LV3 | 5% | Guardian | ≥2258.39 | 
|
-| LV2 | 20% | Knight | ≥1888.252 | 
|
+| LV3 | 5% | Guardian | ≥2265.64 | 
|
+| LV2 | 20% | Knight | ≥1894.28 | 
|
| LV1 | 75% | - | - | - |
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。
@@ -24,6 +24,14 @@
## 往期竞赛
+#### 第 338 场周赛(2023-03-26 10:30, 90 分钟) 参赛人数 5594
+
+- [2600. K 件物品的最大和](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README.md)
+- [2601. 质数减法运算](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README.md)
+- [2602. 使数组元素全部相等的最少操作次数](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README.md)
+- [2603. 收集树中金币](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README.md)
+
+
#### 第 337 场周赛(2023-03-19 10:30, 90 分钟) 参赛人数 5628
- [2595. 奇偶位数](/solution/2500-2599/2595.Number%20of%20Even%20and%20Odd%20Bits/README.md)
diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md
index 6ffeb6420963a..8f8e6f3f8fd13 100644
--- a/solution/CONTEST_README_EN.md
+++ b/solution/CONTEST_README_EN.md
@@ -25,6 +25,14 @@ Get your rating changes right after the completion of LeetCode contests, https:/
## Past Contests
+#### Weekly Contest 338
+
+- [2600. K Items With the Maximum Sum](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README_EN.md)
+- [2601. Prime Subtraction Operation](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README_EN.md)
+- [2602. Minimum Operations to Make All Array Elements Equal](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README_EN.md)
+- [2603. Collect Coins in a Tree](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README_EN.md)
+
+
#### Weekly Contest 337
- [2595. Number of Even and Odd Bits](/solution/2500-2599/2595.Number%20of%20Even%20and%20Odd%20Bits/README_EN.md)
diff --git a/solution/README.md b/solution/README.md
index e83bf44ebc4fd..5d0d0a9bba97d 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -2610,6 +2610,10 @@
| 2597 | [美丽子集的数目](/solution/2500-2599/2597.The%20Number%20of%20Beautiful%20Subsets/README.md) | `数组`,`动态规划`,`回溯` | 中等 | 第 337 场周赛 |
| 2598 | [执行操作后的最大 MEX](/solution/2500-2599/2598.Smallest%20Missing%20Non-negative%20Integer%20After%20Operations/README.md) | `贪心`,`数组`,`哈希表`,`数学` | 中等 | 第 337 场周赛 |
| 2599 | [Make the Prefix Sum Non-negative](/solution/2500-2599/2599.Make%20the%20Prefix%20Sum%20Non-negative/README.md) | | 中等 | 🔒 |
+| 2600 | [K 件物品的最大和](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README.md) | | 简单 | 第 338 场周赛 |
+| 2601 | [质数减法运算](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README.md) | | 中等 | 第 338 场周赛 |
+| 2602 | [使数组元素全部相等的最少操作次数](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README.md) | | 中等 | 第 338 场周赛 |
+| 2603 | [收集树中金币](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README.md) | | 困难 | 第 338 场周赛 |
## 版权
diff --git a/solution/README_EN.md b/solution/README_EN.md
index 21ff9cbd9d99f..ba71941d99760 100644
--- a/solution/README_EN.md
+++ b/solution/README_EN.md
@@ -2608,6 +2608,10 @@ Press Control+F(or Command+F on the
| 2597 | [The Number of Beautiful Subsets](/solution/2500-2599/2597.The%20Number%20of%20Beautiful%20Subsets/README_EN.md) | `Array`,`Dynamic Programming`,`Backtracking` | Medium | Weekly Contest 337 |
| 2598 | [Smallest Missing Non-negative Integer After Operations](/solution/2500-2599/2598.Smallest%20Missing%20Non-negative%20Integer%20After%20Operations/README_EN.md) | `Greedy`,`Array`,`Hash Table`,`Math` | Medium | Weekly Contest 337 |
| 2599 | [Make the Prefix Sum Non-negative](/solution/2500-2599/2599.Make%20the%20Prefix%20Sum%20Non-negative/README_EN.md) | | Medium | 🔒 |
+| 2600 | [K Items With the Maximum Sum](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README_EN.md) | | Easy | Weekly Contest 338 |
+| 2601 | [Prime Subtraction Operation](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README_EN.md) | | Medium | Weekly Contest 338 |
+| 2602 | [Minimum Operations to Make All Array Elements Equal](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README_EN.md) | | Medium | Weekly Contest 338 |
+| 2603 | [Collect Coins in a Tree](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README_EN.md) | | Hard | Weekly Contest 338 |
## Copyright
diff --git a/solution/contest.py b/solution/contest.py
index 12a871efa033c..e619d3b2da151 100644
--- a/solution/contest.py
+++ b/solution/contest.py
@@ -127,8 +127,8 @@ def generate_contest_list():
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
-| LV3 | 5% | Guardian | ≥2258.39 | 
|
-| LV2 | 20% | Knight | ≥1888.252 | 
|
+| LV3 | 5% | Guardian | ≥2265.64 | 
|
+| LV2 | 20% | Knight | ≥1894.28 | 
|
| LV1 | 75% | - | - | - |
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。
diff --git a/solution/summary.md b/solution/summary.md
index 6b169187ad6b3..de58a598073d6 100644
--- a/solution/summary.md
+++ b/solution/summary.md
@@ -2649,3 +2649,9 @@
- [2597.美丽子集的数目](/solution/2500-2599/2597.The%20Number%20of%20Beautiful%20Subsets/README.md)
- [2598.执行操作后的最大 MEX](/solution/2500-2599/2598.Smallest%20Missing%20Non-negative%20Integer%20After%20Operations/README.md)
- [2599.Make the Prefix Sum Non-negative](/solution/2500-2599/2599.Make%20the%20Prefix%20Sum%20Non-negative/README.md)
+
+- 2600-2699
+ - [2600.K 件物品的最大和](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README.md)
+ - [2601.质数减法运算](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README.md)
+ - [2602.使数组元素全部相等的最少操作次数](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README.md)
+ - [2603.收集树中金币](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README.md)
diff --git a/solution/summary_en.md b/solution/summary_en.md
index 08169a0e21468..188d064ae4674 100644
--- a/solution/summary_en.md
+++ b/solution/summary_en.md
@@ -2649,3 +2649,9 @@
- [2597.The Number of Beautiful Subsets](/solution/2500-2599/2597.The%20Number%20of%20Beautiful%20Subsets/README_EN.md)
- [2598.Smallest Missing Non-negative Integer After Operations](/solution/2500-2599/2598.Smallest%20Missing%20Non-negative%20Integer%20After%20Operations/README_EN.md)
- [2599.Make the Prefix Sum Non-negative](/solution/2500-2599/2599.Make%20the%20Prefix%20Sum%20Non-negative/README_EN.md)
+
+- 2600-2699
+ - [2600.K Items With the Maximum Sum](/solution/2600-2699/2600.K%20Items%20With%20the%20Maximum%20Sum/README_EN.md)
+ - [2601.Prime Subtraction Operation](/solution/2600-2699/2601.Prime%20Subtraction%20Operation/README_EN.md)
+ - [2602.Minimum Operations to Make All Array Elements Equal](/solution/2600-2699/2602.Minimum%20Operations%20to%20Make%20All%20Array%20Elements%20Equal/README_EN.md)
+ - [2603.Collect Coins in a Tree](/solution/2600-2699/2603.Collect%20Coins%20in%20a%20Tree/README_EN.md)