diff --git a/solution/0000-0099/0013.Roman to Integer/README.md b/solution/0000-0099/0013.Roman to Integer/README.md index da85ba58aaf09..8f46058741599 100644 --- a/solution/0000-0099/0013.Roman to Integer/README.md +++ b/solution/0000-0099/0013.Roman to Integer/README.md @@ -186,6 +186,28 @@ func romanToInt(s string) int { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function romanToInt($s) { + $hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); + $rs = 0; + for ($i = 0; $i < strlen($s); $i++) { + $left = $hashmap[$s[$i]]; + $right = $hashmap[$s[$i + 1]]; + if ($left >= $right) $rs += $left; + else $rs -= $left; + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0013.Roman to Integer/README_EN.md b/solution/0000-0099/0013.Roman to Integer/README_EN.md index d93738282bc25..9894b2b3951e9 100644 --- a/solution/0000-0099/0013.Roman to Integer/README_EN.md +++ b/solution/0000-0099/0013.Roman to Integer/README_EN.md @@ -164,6 +164,28 @@ func romanToInt(s string) int { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function romanToInt($s) { + $hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); + $rs = 0; + for ($i = 0; $i < strlen($s); $i++) { + $left = $hashmap[$s[$i]]; + $right = $hashmap[$s[$i + 1]]; + if ($left >= $right) $rs += $left; + else $rs -= $left; + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0013.Roman to Integer/Solution.php b/solution/0000-0099/0013.Roman to Integer/Solution.php new file mode 100644 index 0000000000000..4fe99943ef597 --- /dev/null +++ b/solution/0000-0099/0013.Roman to Integer/Solution.php @@ -0,0 +1,17 @@ +class Solution { + /** + * @param String $s + * @return Integer + */ + function romanToInt($s) { + $hashmap = array('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); + $rs = 0; + for ($i = 0; $i < strlen($s); $i++) { + $left = $hashmap[$s[$i]]; + $right = $hashmap[$s[$i + 1]]; + if ($left >= $right) $rs += $left; + else $rs -= $left; + } + return $rs; + } +} \ No newline at end of file diff --git a/solution/0100-0199/0125.Valid Palindrome/README.md b/solution/0100-0199/0125.Valid Palindrome/README.md index f301362baa837..c53645aed491e 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README.md +++ b/solution/0100-0199/0125.Valid Palindrome/README.md @@ -310,7 +310,7 @@ func verify(ch byte) bool { ### **PHP** -```php +```php class Solution { /** * @param String $s diff --git a/solution/0100-0199/0125.Valid Palindrome/README_EN.md b/solution/0100-0199/0125.Valid Palindrome/README_EN.md index 8f6a681034ac0..38d356d454fb3 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README_EN.md +++ b/solution/0100-0199/0125.Valid Palindrome/README_EN.md @@ -298,7 +298,7 @@ func verify(ch byte) bool { ### **PHP** -```php +```php class Solution { /** * @param String $s diff --git a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md index 11a36ca1d9b66..89901c30db03a 100644 --- a/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md +++ b/solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md @@ -6,7 +6,7 @@

You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.

-

Find the maximum profit you can achieve. You may complete at most k transactions.

+

Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

diff --git a/solution/0200-0299/0211.Design Add and Search Words Data Structure/README_EN.md b/solution/0200-0299/0211.Design Add and Search Words Data Structure/README_EN.md index 155e62d6a8547..44a7f9b2d39f7 100644 --- a/solution/0200-0299/0211.Design Add and Search Words Data Structure/README_EN.md +++ b/solution/0200-0299/0211.Design Add and Search Words Data Structure/README_EN.md @@ -42,7 +42,7 @@ wordDictionary.search("b.."); // return True
  • 1 <= word.length <= 25
  • word in addWord consists of lowercase English letters.
  • word in search consist of '.' or lowercase English letters.
  • -
  • There will be at most 3 dots in word for search queries.
  • +
  • There will be at most 2 dots in word for search queries.
  • At most 104 calls will be made to addWord and search.
  • diff --git a/solution/1700-1799/1734.Decode XORed Permutation/README.md b/solution/1700-1799/1734.Decode XORed Permutation/README.md index d237d1981212e..0d734544e00a4 100644 --- a/solution/1700-1799/1734.Decode XORed Permutation/README.md +++ b/solution/1700-1799/1734.Decode XORed Permutation/README.md @@ -41,6 +41,12 @@ +**方法一:位运算** + +我们注意到,数组 $perm$ 是前 $n$ 个正整数的排列,因此 $perm$ 的所有元素的异或和为 $1 \oplus 2 \oplus \cdots \oplus n$,记为 $a$。而 $encode[i]=perm[i] \oplus perm[i+1]$,如果我们将 $encode[0],encode[2],\cdots,encode[n-3]$ 的所有元素的异或和记为 $b$,则 $perm[n-1]=a \oplus b$。知道了 $perm$ 的最后一个元素,我们就可以通过逆序遍历数组 $encode$ 求出 $perm$ 的所有元素。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $perm$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 + ### **Python3** @@ -54,12 +60,13 @@ class Solution: a = b = 0 for i in range(0, n - 1, 2): a ^= encoded[i] - for i in range(n + 1): + for i in range(1, n + 1): b ^= i - ans = [a ^ b] - for e in encoded[::-1]: - ans.append(ans[-1] ^ e) - return ans[::-1] + perm = [0] * n + perm[-1] = a ^ b + for i in range(n - 2, -1, -1): + perm[i] = encoded[i] ^ perm[i + 1] + return perm ``` ### **Java** @@ -68,23 +75,21 @@ class Solution: ```java class Solution { - public int[] decode(int[] encoded) { int n = encoded.length + 1; - int[] ans = new int[n]; - int a = 0; - int b = 0; + int a = 0, b = 0; for (int i = 0; i < n - 1; i += 2) { a ^= encoded[i]; } - for (int i = 0; i < n + 1; ++i) { + for (int i = 1; i <= n; ++i) { b ^= i; } - ans[n - 1] = a ^ b; + int[] perm = new int[n]; + perm[n - 1] = a ^ b; for (int i = n - 2; i >= 0; --i) { - ans[i] = ans[i + 1] ^ encoded[i]; + perm[i] = encoded[i] ^ perm[i + 1]; } - return ans; + return perm; } } ``` @@ -96,13 +101,19 @@ class Solution { public: vector decode(vector& encoded) { int n = encoded.size() + 1; - vector ans(n); int a = 0, b = 0; - for (int i = 0; i < n - 1; i += 2) a ^= encoded[i]; - for (int i = 0; i < n + 1; ++i) b ^= i; - ans[n - 1] = a ^ b; - for (int i = n - 2; i >= 0; --i) ans[i] = ans[i + 1] ^ encoded[i]; - return ans; + for (int i = 0; i < n - 1; i += 2) { + a ^= encoded[i]; + } + for (int i = 1; i <= n; ++i) { + b ^= i; + } + vector perm(n); + perm[n - 1] = a ^ b; + for (int i = n - 2; ~i; --i) { + perm[i] = encoded[i] ^ perm[i + 1]; + } + return perm; } }; ``` @@ -112,19 +123,19 @@ public: ```go func decode(encoded []int) []int { n := len(encoded) + 1 - ans := make([]int, n) a, b := 0, 0 for i := 0; i < n-1; i += 2 { a ^= encoded[i] } - for i := 0; i < n+1; i++ { + for i := 1; i <= n; i++ { b ^= i } - ans[n-1] = a ^ b + perm := make([]int, n) + perm[n-1] = a ^ b for i := n - 2; i >= 0; i-- { - ans[i] = ans[i+1] ^ encoded[i] + perm[i] = encoded[i] ^ perm[i+1] } - return ans + return perm } ``` diff --git a/solution/1700-1799/1734.Decode XORed Permutation/README_EN.md b/solution/1700-1799/1734.Decode XORed Permutation/README_EN.md index fe5aada7ee333..c51b9acf5ce5d 100644 --- a/solution/1700-1799/1734.Decode XORed Permutation/README_EN.md +++ b/solution/1700-1799/1734.Decode XORed Permutation/README_EN.md @@ -48,12 +48,13 @@ class Solution: a = b = 0 for i in range(0, n - 1, 2): a ^= encoded[i] - for i in range(n + 1): + for i in range(1, n + 1): b ^= i - ans = [a ^ b] - for e in encoded[::-1]: - ans.append(ans[-1] ^ e) - return ans[::-1] + perm = [0] * n + perm[-1] = a ^ b + for i in range(n - 2, -1, -1): + perm[i] = encoded[i] ^ perm[i + 1] + return perm ``` ### **Java** @@ -62,20 +63,19 @@ class Solution: class Solution { public int[] decode(int[] encoded) { int n = encoded.length + 1; - int[] ans = new int[n]; - int a = 0; - int b = 0; + int a = 0, b = 0; for (int i = 0; i < n - 1; i += 2) { a ^= encoded[i]; } - for (int i = 0; i < n + 1; ++i) { + for (int i = 1; i <= n; ++i) { b ^= i; } - ans[n - 1] = a ^ b; + int[] perm = new int[n]; + perm[n - 1] = a ^ b; for (int i = n - 2; i >= 0; --i) { - ans[i] = ans[i + 1] ^ encoded[i]; + perm[i] = encoded[i] ^ perm[i + 1]; } - return ans; + return perm; } } ``` @@ -87,13 +87,19 @@ class Solution { public: vector decode(vector& encoded) { int n = encoded.size() + 1; - vector ans(n); int a = 0, b = 0; - for (int i = 0; i < n - 1; i += 2) a ^= encoded[i]; - for (int i = 0; i < n + 1; ++i) b ^= i; - ans[n - 1] = a ^ b; - for (int i = n - 2; i >= 0; --i) ans[i] = ans[i + 1] ^ encoded[i]; - return ans; + for (int i = 0; i < n - 1; i += 2) { + a ^= encoded[i]; + } + for (int i = 1; i <= n; ++i) { + b ^= i; + } + vector perm(n); + perm[n - 1] = a ^ b; + for (int i = n - 2; ~i; --i) { + perm[i] = encoded[i] ^ perm[i + 1]; + } + return perm; } }; ``` @@ -103,19 +109,19 @@ public: ```go func decode(encoded []int) []int { n := len(encoded) + 1 - ans := make([]int, n) a, b := 0, 0 for i := 0; i < n-1; i += 2 { a ^= encoded[i] } - for i := 0; i < n+1; i++ { + for i := 1; i <= n; i++ { b ^= i } - ans[n-1] = a ^ b + perm := make([]int, n) + perm[n-1] = a ^ b for i := n - 2; i >= 0; i-- { - ans[i] = ans[i+1] ^ encoded[i] + perm[i] = encoded[i] ^ perm[i+1] } - return ans + return perm } ``` diff --git a/solution/1700-1799/1734.Decode XORed Permutation/Solution.cpp b/solution/1700-1799/1734.Decode XORed Permutation/Solution.cpp index 30fe642034e1f..572d6ef1a22dc 100644 --- a/solution/1700-1799/1734.Decode XORed Permutation/Solution.cpp +++ b/solution/1700-1799/1734.Decode XORed Permutation/Solution.cpp @@ -2,12 +2,18 @@ class Solution { public: vector decode(vector& encoded) { int n = encoded.size() + 1; - vector ans(n); int a = 0, b = 0; - for (int i = 0; i < n - 1; i += 2) a ^= encoded[i]; - for (int i = 0; i < n + 1; ++i) b ^= i; - ans[n - 1] = a ^ b; - for (int i = n - 2; i >= 0; --i) ans[i] = ans[i + 1] ^ encoded[i]; - return ans; + for (int i = 0; i < n - 1; i += 2) { + a ^= encoded[i]; + } + for (int i = 1; i <= n; ++i) { + b ^= i; + } + vector perm(n); + perm[n - 1] = a ^ b; + for (int i = n - 2; ~i; --i) { + perm[i] = encoded[i] ^ perm[i + 1]; + } + return perm; } }; \ No newline at end of file diff --git a/solution/1700-1799/1734.Decode XORed Permutation/Solution.go b/solution/1700-1799/1734.Decode XORed Permutation/Solution.go index 0b7898386dcc0..abf1f7c0b6ee3 100644 --- a/solution/1700-1799/1734.Decode XORed Permutation/Solution.go +++ b/solution/1700-1799/1734.Decode XORed Permutation/Solution.go @@ -1,16 +1,16 @@ func decode(encoded []int) []int { n := len(encoded) + 1 - ans := make([]int, n) a, b := 0, 0 for i := 0; i < n-1; i += 2 { a ^= encoded[i] } - for i := 0; i < n+1; i++ { + for i := 1; i <= n; i++ { b ^= i } - ans[n-1] = a ^ b + perm := make([]int, n) + perm[n-1] = a ^ b for i := n - 2; i >= 0; i-- { - ans[i] = ans[i+1] ^ encoded[i] + perm[i] = encoded[i] ^ perm[i+1] } - return ans + return perm } \ No newline at end of file diff --git a/solution/1700-1799/1734.Decode XORed Permutation/Solution.java b/solution/1700-1799/1734.Decode XORed Permutation/Solution.java index 4fddf8ddc9d14..73eb0356f0591 100644 --- a/solution/1700-1799/1734.Decode XORed Permutation/Solution.java +++ b/solution/1700-1799/1734.Decode XORed Permutation/Solution.java @@ -1,19 +1,18 @@ class Solution { public int[] decode(int[] encoded) { int n = encoded.length + 1; - int[] ans = new int[n]; - int a = 0; - int b = 0; + int a = 0, b = 0; for (int i = 0; i < n - 1; i += 2) { a ^= encoded[i]; } - for (int i = 0; i < n + 1; ++i) { + for (int i = 1; i <= n; ++i) { b ^= i; } - ans[n - 1] = a ^ b; + int[] perm = new int[n]; + perm[n - 1] = a ^ b; for (int i = n - 2; i >= 0; --i) { - ans[i] = ans[i + 1] ^ encoded[i]; + perm[i] = encoded[i] ^ perm[i + 1]; } - return ans; + return perm; } } \ No newline at end of file diff --git a/solution/1700-1799/1734.Decode XORed Permutation/Solution.py b/solution/1700-1799/1734.Decode XORed Permutation/Solution.py index 5fc8f7d71f172..164d529717a48 100644 --- a/solution/1700-1799/1734.Decode XORed Permutation/Solution.py +++ b/solution/1700-1799/1734.Decode XORed Permutation/Solution.py @@ -4,9 +4,10 @@ def decode(self, encoded: List[int]) -> List[int]: a = b = 0 for i in range(0, n - 1, 2): a ^= encoded[i] - for i in range(n + 1): + for i in range(1, n + 1): b ^= i - ans = [a ^ b] - for e in encoded[::-1]: - ans.append(ans[-1] ^ e) - return ans[::-1] + perm = [0] * n + perm[-1] = a ^ b + for i in range(n - 2, -1, -1): + perm[i] = encoded[i] ^ perm[i + 1] + return perm diff --git a/solution/1700-1799/1735.Count Ways to Make Array With Product/README.md b/solution/1700-1799/1735.Count Ways to Make Array With Product/README.md index 7795104024a88..d7f3f9c1194d8 100644 --- a/solution/1700-1799/1735.Count Ways to Make Array With Product/README.md +++ b/solution/1700-1799/1735.Count Ways to Make Array With Product/README.md @@ -43,6 +43,22 @@ +**方法一:质因数分解 + 组合数学** + +我们可以对 $k$ 进行质因数分解,即 $k = p_1^{x_1} \times p_2^{x_2} \times \cdots \times p_m^{x_m}$,其中 $p_i$ 为质数,而 $x_i$ 为 $p_i$ 的指数。那么题目实际上等价于:把 $x_1$ 个 $p_1$, $x_2$ 个 $p_2$, $\cdots$, $x_m$ 个 $p_m$ 分别放到 $n$ 个位置上,单个位置可以为空,问有多少种方案。 + +根据组合数学的知识,我们把 $x$ 个球放入 $n$ 个盒子中,有两种情况: + +如果盒子不能为空,那么方案数为 $C_{x-1}^{n-1}$,这里是利用隔板法,即一共 $x$ 个球,我们在其中 $x-1$ 个位置插入 $n-1$ 个隔板,从而将 $x$ 个球分成 $n$ 组。 + +如果盒子可以为空,那么我们可以再增加 $n$ 个虚拟球,然后再利用隔板法,即一共 $x+n$ 个球,我们在其中 $x+n-1$ 个位置插入 $n-1$ 个隔板,从而将实际的 $x$ 个球分成 $n$ 组,并且允许盒子为空,因此方案数为 $C_{x+n-1}^{n-1}$。 + +因此,对于每个查询 $queries[i]$,我们可以先对 $k$ 进行质因数分解,得到指数 $x_1, x_2, \cdots, x_m$,然后计算 $C_{x_1+n-1}^{n-1}, C_{x_2+n-1}^{n-1}, \cdots, C_{x_m+n-1}^{n-1}$,最后将所有的方案数相乘即可。 + +所以,问题转化为如何快速计算 $C_m^n$,根据公式 $C_m^n = \frac{m!}{n!(m-n)!}$,我们可以先预处理出 $m!$,然后利用逆元快速计算 $C_m^n$。 + +时间复杂度 $O(K \times \log \log K + N + m \times \log K)$,空间复杂度 $O(N)$。 + ### **Python3** @@ -50,7 +66,41 @@ ```python +N = 10020 +MOD = 10**9 + 7 +f = [1] * N +g = [1] * N +p = defaultdict(list) +for i in range(1, N): + f[i] = f[i - 1] * i % MOD + g[i] = pow(f[i], MOD - 2, MOD) + x = i + j = 2 + while j <= x // j: + if x % j == 0: + cnt = 0 + while x % j == 0: + cnt += 1 + x //= j + p[i].append(cnt) + j += 1 + if x > 1: + p[i].append(1) + +def comb(n, k): + return f[n] * g[k] * g[n - k] % MOD + + +class Solution: + def waysToFillArray(self, queries: List[List[int]]) -> List[int]: + ans = [] + for n, k in queries: + t = 1 + for x in p[k]: + t = t * comb(x + n - 1, n - 1) % MOD + ans.append(t) + return ans ``` ### **Java** @@ -58,7 +108,195 @@ ```java +class Solution { + private static final int N = 10020; + private static final int MOD = (int) 1e9 + 7; + private static final long[] F = new long[N]; + private static final long[] G = new long[N]; + private static final List[] P = new List[N]; + + static { + F[0] = 1; + G[0] = 1; + Arrays.setAll(P, k -> new ArrayList<>()); + for (int i = 1; i < N; ++i) { + F[i] = F[i - 1] * i % MOD; + G[i] = qmi(F[i], MOD - 2, MOD); + int x = i; + for (int j = 2; j <= x / j; ++j) { + if (x % j == 0) { + int cnt = 0; + while (x % j == 0) { + ++cnt; + x /= j; + } + P[i].add(cnt); + } + } + if (x > 1) { + P[i].add(1); + } + } + } + + public static long qmi(long a, long k, long p) { + long res = 1; + while (k != 0) { + if ((k & 1) == 1) { + res = res * a % p; + } + k >>= 1; + a = a * a % p; + } + return res; + } + + public static long comb(int n, int k) { + return (F[n] * G[k] % MOD) * G[n - k] % MOD; + } + + + public int[] waysToFillArray(int[][] queries) { + int m = queries.length; + int[] ans = new int[m]; + for (int i = 0; i < m; ++i) { + int n = queries[i][0], k = queries[i][1]; + long t = 1; + for (int x : P[k]) { + t = t * comb(x + n - 1, n - 1) % MOD; + } + ans[i] = (int) t; + } + return ans; + } +} +``` + +### **C++** + +```cpp +int N = 10020; +int MOD = 1e9 + 7; +long f[10020]; +long g[10020]; +vector p[10020]; + +long qmi(long a, long k, long p) { + long res = 1; + while (k != 0) { + if ((k & 1) == 1) { + res = res * a % p; + } + k >>= 1; + a = a * a % p; + } + return res; +} + +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); + int x = i; + for (int j = 2; j <= x / j; ++j) { + if (x % j == 0) { + int cnt = 0; + while (x % j == 0) { + ++cnt; + x /= j; + } + p[i].push_back(cnt); + } + } + if (x > 1) { + p[i].push_back(1); + } + } + return 0; +}(); + +int comb(int n, int k) { + return (f[n] * g[k] % MOD) * g[n - k] % MOD; +} + +class Solution { +public: + vector waysToFillArray(vector>& queries) { + vector ans; + for (auto& q : queries) { + int n = q[0], k = q[1]; + long long t = 1; + for (int x : p[k]) { + t = t * comb(x + n - 1, n - 1) % MOD; + } + ans.push_back(t); + } + return ans; + } +}; +``` + +### **Go** + +```go +const n = 1e4 + 20 +const mod = 1e9 + 7 + +var f = make([]int, n) +var g = make([]int, n) +var p = make([][]int, n) + +func qmi(a, k, p int) int { + res := 1 + for k != 0 { + if k&1 == 1 { + res = res * a % p + } + k >>= 1 + a = a * a % p + } + return res +} + +func init() { + f[0], g[0] = 1, 1 + for i := 1; i < n; i++ { + f[i] = f[i-1] * i % mod + g[i] = qmi(f[i], mod-2, mod) + x := i + for j := 2; j <= x/j; j++ { + if x%j == 0 { + cnt := 0 + for x%j == 0 { + cnt++ + x /= j + } + p[i] = append(p[i], cnt) + } + } + if x > 1 { + p[i] = append(p[i], 1) + } + } +} + +func comb(n, k int) int { + return (f[n] * g[k] % mod) * g[n-k] % mod +} +func waysToFillArray(queries [][]int) (ans []int) { + for _, q := range queries { + n, k := q[0], q[1] + t := 1 + for _, x := range p[k] { + t = t * comb(x+n-1, n-1) % mod + } + ans = append(ans, t) + } + return +} ``` ### **...** diff --git a/solution/1700-1799/1735.Count Ways to Make Array With Product/README_EN.md b/solution/1700-1799/1735.Count Ways to Make Array With Product/README_EN.md index 9077aff4064f0..baa46fae49958 100644 --- a/solution/1700-1799/1735.Count Ways to Make Array With Product/README_EN.md +++ b/solution/1700-1799/1735.Count Ways to Make Array With Product/README_EN.md @@ -42,13 +42,235 @@ ### **Python3** ```python +N = 10020 +MOD = 10**9 + 7 +f = [1] * N +g = [1] * N +p = defaultdict(list) +for i in range(1, N): + f[i] = f[i - 1] * i % MOD + g[i] = pow(f[i], MOD - 2, MOD) + x = i + j = 2 + while j <= x // j: + if x % j == 0: + cnt = 0 + while x % j == 0: + cnt += 1 + x //= j + p[i].append(cnt) + j += 1 + if x > 1: + p[i].append(1) + +def comb(n, k): + return f[n] * g[k] * g[n - k] % MOD + + +class Solution: + def waysToFillArray(self, queries: List[List[int]]) -> List[int]: + ans = [] + for n, k in queries: + t = 1 + for x in p[k]: + t = t * comb(x + n - 1, n - 1) % MOD + ans.append(t) + return ans ``` ### **Java** ```java +class Solution { + private static final int N = 10020; + private static final int MOD = (int) 1e9 + 7; + private static final long[] F = new long[N]; + private static final long[] G = new long[N]; + private static final List[] P = new List[N]; + + static { + F[0] = 1; + G[0] = 1; + Arrays.setAll(P, k -> new ArrayList<>()); + for (int i = 1; i < N; ++i) { + F[i] = F[i - 1] * i % MOD; + G[i] = qmi(F[i], MOD - 2, MOD); + int x = i; + for (int j = 2; j <= x / j; ++j) { + if (x % j == 0) { + int cnt = 0; + while (x % j == 0) { + ++cnt; + x /= j; + } + P[i].add(cnt); + } + } + if (x > 1) { + P[i].add(1); + } + } + } + + public static long qmi(long a, long k, long p) { + long res = 1; + while (k != 0) { + if ((k & 1) == 1) { + res = res * a % p; + } + k >>= 1; + a = a * a % p; + } + return res; + } + + public static long comb(int n, int k) { + return (F[n] * G[k] % MOD) * G[n - k] % MOD; + } + + + public int[] waysToFillArray(int[][] queries) { + int m = queries.length; + int[] ans = new int[m]; + for (int i = 0; i < m; ++i) { + int n = queries[i][0], k = queries[i][1]; + long t = 1; + for (int x : P[k]) { + t = t * comb(x + n - 1, n - 1) % MOD; + } + ans[i] = (int) t; + } + return ans; + } +} +``` + +### **C++** + +```cpp +int N = 10020; +int MOD = 1e9 + 7; +long f[10020]; +long g[10020]; +vector p[10020]; + +long qmi(long a, long k, long p) { + long res = 1; + while (k != 0) { + if ((k & 1) == 1) { + res = res * a % p; + } + k >>= 1; + a = a * a % p; + } + return res; +} + +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); + int x = i; + for (int j = 2; j <= x / j; ++j) { + if (x % j == 0) { + int cnt = 0; + while (x % j == 0) { + ++cnt; + x /= j; + } + p[i].push_back(cnt); + } + } + if (x > 1) { + p[i].push_back(1); + } + } + return 0; +}(); + +int comb(int n, int k) { + return (f[n] * g[k] % MOD) * g[n - k] % MOD; +} + +class Solution { +public: + vector waysToFillArray(vector>& queries) { + vector ans; + for (auto& q : queries) { + int n = q[0], k = q[1]; + long long t = 1; + for (int x : p[k]) { + t = t * comb(x + n - 1, n - 1) % MOD; + } + ans.push_back(t); + } + return ans; + } +}; +``` + +### **Go** + +```go +const n = 1e4 + 20 +const mod = 1e9 + 7 + +var f = make([]int, n) +var g = make([]int, n) +var p = make([][]int, n) + +func qmi(a, k, p int) int { + res := 1 + for k != 0 { + if k&1 == 1 { + res = res * a % p + } + k >>= 1 + a = a * a % p + } + return res +} + +func init() { + f[0], g[0] = 1, 1 + for i := 1; i < n; i++ { + f[i] = f[i-1] * i % mod + g[i] = qmi(f[i], mod-2, mod) + x := i + for j := 2; j <= x/j; j++ { + if x%j == 0 { + cnt := 0 + for x%j == 0 { + cnt++ + x /= j + } + p[i] = append(p[i], cnt) + } + } + if x > 1 { + p[i] = append(p[i], 1) + } + } +} + +func comb(n, k int) int { + return (f[n] * g[k] % mod) * g[n-k] % mod +} +func waysToFillArray(queries [][]int) (ans []int) { + for _, q := range queries { + n, k := q[0], q[1] + t := 1 + for _, x := range p[k] { + t = t * comb(x+n-1, n-1) % mod + } + ans = append(ans, t) + } + return +} ``` ### **...** diff --git a/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.cpp b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.cpp new file mode 100644 index 0000000000000..c1fbab137d9ac --- /dev/null +++ b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.cpp @@ -0,0 +1,61 @@ +int N = 10020; +int MOD = 1e9 + 7; +long f[10020]; +long g[10020]; +vector p[10020]; + +long qmi(long a, long k, long p) { + long res = 1; + while (k != 0) { + if ((k & 1) == 1) { + res = res * a % p; + } + k >>= 1; + a = a * a % p; + } + return res; +} + +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); + int x = i; + for (int j = 2; j <= x / j; ++j) { + if (x % j == 0) { + int cnt = 0; + while (x % j == 0) { + ++cnt; + x /= j; + } + p[i].push_back(cnt); + } + } + if (x > 1) { + p[i].push_back(1); + } + } + return 0; +}(); + +int comb(int n, int k) { + return (f[n] * g[k] % MOD) * g[n - k] % MOD; +} + +class Solution { +public: + vector waysToFillArray(vector>& queries) { + vector ans; + for (auto& q : queries) { + int n = q[0], k = q[1]; + long long t = 1; + for (int x : p[k]) { + t = t * comb(x + n - 1, n - 1) % MOD; + } + ans.push_back(t); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.go b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.go new file mode 100644 index 0000000000000..9e9e12c1d6188 --- /dev/null +++ b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.go @@ -0,0 +1,56 @@ +const n = 1e4 + 20 +const mod = 1e9 + 7 + +var f = make([]int, n) +var g = make([]int, n) +var p = make([][]int, n) + +func qmi(a, k, p int) int { + res := 1 + for k != 0 { + if k&1 == 1 { + res = res * a % p + } + k >>= 1 + a = a * a % p + } + return res +} + +func init() { + f[0], g[0] = 1, 1 + for i := 1; i < n; i++ { + f[i] = f[i-1] * i % mod + g[i] = qmi(f[i], mod-2, mod) + x := i + for j := 2; j <= x/j; j++ { + if x%j == 0 { + cnt := 0 + for x%j == 0 { + cnt++ + x /= j + } + p[i] = append(p[i], cnt) + } + } + if x > 1 { + p[i] = append(p[i], 1) + } + } +} + +func comb(n, k int) int { + return (f[n] * g[k] % mod) * g[n-k] % mod +} + +func waysToFillArray(queries [][]int) (ans []int) { + for _, q := range queries { + n, k := q[0], q[1] + t := 1 + for _, x := range p[k] { + t = t * comb(x+n-1, n-1) % mod + } + ans = append(ans, t) + } + return +} \ No newline at end of file diff --git a/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.java b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.java new file mode 100644 index 0000000000000..693beac390574 --- /dev/null +++ b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.java @@ -0,0 +1,62 @@ +class Solution { + private static final int N = 10020; + private static final int MOD = (int) 1e9 + 7; + private static final long[] F = new long[N]; + private static final long[] G = new long[N]; + private static final List[] P = new List[N]; + + static { + F[0] = 1; + G[0] = 1; + Arrays.setAll(P, k -> new ArrayList<>()); + for (int i = 1; i < N; ++i) { + F[i] = F[i - 1] * i % MOD; + G[i] = qmi(F[i], MOD - 2, MOD); + int x = i; + for (int j = 2; j <= x / j; ++j) { + if (x % j == 0) { + int cnt = 0; + while (x % j == 0) { + ++cnt; + x /= j; + } + P[i].add(cnt); + } + } + if (x > 1) { + P[i].add(1); + } + } + } + + public static long qmi(long a, long k, long p) { + long res = 1; + while (k != 0) { + if ((k & 1) == 1) { + res = res * a % p; + } + k >>= 1; + a = a * a % p; + } + return res; + } + + public static long comb(int n, int k) { + return (F[n] * G[k] % MOD) * G[n - k] % MOD; + } + + + public int[] waysToFillArray(int[][] queries) { + int m = queries.length; + int[] ans = new int[m]; + for (int i = 0; i < m; ++i) { + int n = queries[i][0], k = queries[i][1]; + long t = 1; + for (int x : P[k]) { + t = t * comb(x + n - 1, n - 1) % MOD; + } + ans[i] = (int) t; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.py b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.py new file mode 100644 index 0000000000000..ccb0dd8e02f76 --- /dev/null +++ b/solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.py @@ -0,0 +1,35 @@ +N = 10020 +MOD = 10**9 + 7 +f = [1] * N +g = [1] * N +p = defaultdict(list) +for i in range(1, N): + f[i] = f[i - 1] * i % MOD + g[i] = pow(f[i], MOD - 2, MOD) + x = i + j = 2 + while j <= x // j: + if x % j == 0: + cnt = 0 + while x % j == 0: + cnt += 1 + x //= j + p[i].append(cnt) + j += 1 + if x > 1: + p[i].append(1) + + +def comb(n, k): + return f[n] * g[k] * g[n - k] % MOD + + +class Solution: + def waysToFillArray(self, queries: List[List[int]]) -> List[int]: + ans = [] + for n, k in queries: + t = 1 + for x in p[k]: + t = t * comb(x + n - 1, n - 1) % MOD + ans.append(t) + return ans diff --git a/solution/2300-2399/2395.Find Subarrays With Equal Sum/README.md b/solution/2300-2399/2395.Find Subarrays With Equal Sum/README.md index 7663601bcc706..a8cbfaf03dcc5 100644 --- a/solution/2300-2399/2395.Find Subarrays With Equal Sum/README.md +++ b/solution/2300-2399/2395.Find Subarrays With Equal Sum/README.md @@ -143,6 +143,39 @@ function findSubarrays(nums: number[]): boolean { } ``` +### **Rust** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn find_subarrays(nums: Vec) -> bool { + let n = nums.len(); + let mut set = HashSet::new(); + for i in 1..n { + if !set.insert(nums[i - 1] + nums[i]) { + return true; + } + } + false + } +} +``` + +### **C** + +```c +bool findSubarrays(int *nums, int numsSize) { + for (int i = 1; i < numsSize - 1; i++) { + for (int j = i + 1; j < numsSize; j++) { + if (nums[i - 1] + nums[i] == nums[j - 1] + nums[j]) { + return true; + } + } + } + return false; +} +``` + ### **...** ``` diff --git a/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md b/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md index 0c26b08c2b1b5..6fc0ea7f6b9b4 100644 --- a/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md +++ b/solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md @@ -128,6 +128,39 @@ function findSubarrays(nums: number[]): boolean { } ``` +### **Rust** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn find_subarrays(nums: Vec) -> bool { + let n = nums.len(); + let mut set = HashSet::new(); + for i in 1..n { + if !set.insert(nums[i - 1] + nums[i]) { + return true; + } + } + false + } +} +``` + +### **C** + +```c +bool findSubarrays(int *nums, int numsSize) { + for (int i = 1; i < numsSize - 1; i++) { + for (int j = i + 1; j < numsSize; j++) { + if (nums[i - 1] + nums[i] == nums[j - 1] + nums[j]) { + return true; + } + } + } + return false; +} +``` + ### **...** ``` diff --git a/solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.c b/solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.c new file mode 100644 index 0000000000000..ca321d320584e --- /dev/null +++ b/solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.c @@ -0,0 +1,10 @@ +bool findSubarrays(int *nums, int numsSize) { + for (int i = 1; i < numsSize - 2; i++) { + for (int j = i + 1; j < numsSize; j++) { + if (nums[i - 1] + nums[i] == nums[j - 1] + nums[j]) { + return true; + } + } + } + return false; +} diff --git a/solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.rs b/solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.rs new file mode 100644 index 0000000000000..413fb067833e1 --- /dev/null +++ b/solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.rs @@ -0,0 +1,13 @@ +use std::collections::HashSet; +impl Solution { + pub fn find_subarrays(nums: Vec) -> bool { + let n = nums.len(); + let mut set = HashSet::new(); + for i in 1..n { + if !set.insert(nums[i - 1] + nums[i]) { + return true; + } + } + false + } +} diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md b/solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md index 792f6d24e0cc0..ad1368a6dc23a 100644 --- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md +++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md @@ -52,11 +52,13 @@ -**方法一:DFS 或二进制枚举** +**方法一:二进制枚举** -直接二进制枚举选中的列,然后判断是否覆盖所有行中的 `1`,若是,更新答案。 +我们先将矩阵中的每一行转换成一个二进制数,记录在数组 $rows$ 中,其中 $rows[i]$ 表示第 $i$ 行对应的二进制数,而 $rows[i]$ 的第 $j$ 位表示第 $i$ 行第 $j$ 列的值。 -时间复杂度 $O(2^n\times n)$,空间复杂度 $O(m)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 +接下来,我们枚举所有的 $2^n$ 种列选择方案,其中 $n$ 为矩阵的列数。对于每一种列选择方案,我们判断是否选中了 $numSelect$ 列,如果不是,则跳过。否则,我们统计矩阵中有多少行中的所有 $1$ 都被选中的列覆盖,即统计有多少行的二进制数 $rows[i]$ 与列选择方案 $mask$ 按位与的结果等于 $rows[i]$,并更新最大的行数。 + +时间复杂度 $O(2^n \times m)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 @@ -66,43 +68,17 @@ ```python class Solution: - def maximumRows(self, mat: List[List[int]], cols: int) -> int: - def dfs(mask, i): - if i > n or mask.bit_count() > cols: - return - nonlocal ans - if i == n: - t = sum((v & mask) == v for v in arr) - ans = max(ans, t) - return - dfs(mask, i + 1) - dfs(mask | 1 << i, i + 1) - - arr = [] - ans, n = 0, len(mat[0]) - for i, row in enumerate(mat): - x = 0 - for j, v in enumerate(row): - x |= v << j - arr.append(x) - dfs(0, 0) - return ans -``` - -```python -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 ``` @@ -113,25 +89,24 @@ class Solution: ```java 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; } } @@ -147,19 +122,26 @@ class Solution { ```cpp class Solution { public: - int maximumRows(vector>& mat, int cols) { - int m = mat.size(), n = mat[0].size(); - vector arr(m); + int maximumRows(vector>& matrix, int numSelect) { + int m = matrix.size(), n = matrix[0].size(); + int rows[m]; + memset(rows, 0, sizeof(rows)); for (int i = 0; i < m; ++i) { - int x = 0; - for (int j = 0; j < n; ++j) x |= mat[i][j] << j; - arr[i] = x; + for (int j = 0; j < n; ++j) { + if (matrix[i][j]) { + rows[i] |= 1 << j; + } + } } int ans = 0; - for (int mask = 1; mask <= 1 << n; ++mask) { - if (__builtin_popcount(mask) > cols) continue; + for (int mask = 1; mask < 1 << n; ++mask) { + if (__builtin_popcount(mask) != numSelect) { + continue; + } int t = 0; - for (int v : arr) t += (v & mask) == v; + for (int x : rows) { + t += (x & mask) == x; + } ans = max(ans, t); } return ans; @@ -170,37 +152,31 @@ public: ### **Go** ```go -func maximumRows(mat [][]int, cols int) int { - m, n := len(mat), len(mat[0]) - arr := make([]int, m) - for i, row := range mat { - x := 0 - for j, v := range row { - x |= v << j +func maximumRows(matrix [][]int, numSelect int) (ans int) { + m, n := len(matrix), len(matrix[0]) + rows := make([]int, m) + for i, row := range matrix { + for j, x := range row { + if x == 1 { + rows[i] |= 1 << j + } } - arr[i] = x } - ans := 0 - for mask := 1; mask <= 1< b { - return a + if ans < t { + ans = t + } } - return b + return } ``` diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md b/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md index 56e4f623fe9e5..7623681ab1a49 100644 --- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md +++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md @@ -61,43 +61,17 @@ Therefore, we return 2. ```python class Solution: - def maximumRows(self, mat: List[List[int]], cols: int) -> int: - def dfs(mask, i): - if i > n or mask.bit_count() > cols: - return - nonlocal ans - if i == n: - t = sum((v & mask) == v for v in arr) - ans = max(ans, t) - return - dfs(mask, i + 1) - dfs(mask | 1 << i, i + 1) - - arr = [] - ans, n = 0, len(mat[0]) - for i, row in enumerate(mat): - x = 0 - for j, v in enumerate(row): - x |= v << j - arr.append(x) - dfs(0, 0) - return ans -``` - -```python -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 ``` @@ -106,25 +80,24 @@ class Solution: ```java 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; } } @@ -140,19 +113,26 @@ class Solution { ```cpp class Solution { public: - int maximumRows(vector>& mat, int cols) { - int m = mat.size(), n = mat[0].size(); - vector arr(m); + int maximumRows(vector>& matrix, int numSelect) { + int m = matrix.size(), n = matrix[0].size(); + int rows[m]; + memset(rows, 0, sizeof(rows)); for (int i = 0; i < m; ++i) { - int x = 0; - for (int j = 0; j < n; ++j) x |= mat[i][j] << j; - arr[i] = x; + for (int j = 0; j < n; ++j) { + if (matrix[i][j]) { + rows[i] |= 1 << j; + } + } } int ans = 0; - for (int mask = 1; mask <= 1 << n; ++mask) { - if (__builtin_popcount(mask) > cols) continue; + for (int mask = 1; mask < 1 << n; ++mask) { + if (__builtin_popcount(mask) != numSelect) { + continue; + } int t = 0; - for (int v : arr) t += (v & mask) == v; + for (int x : rows) { + t += (x & mask) == x; + } ans = max(ans, t); } return ans; @@ -163,37 +143,31 @@ public: ### **Go** ```go -func maximumRows(mat [][]int, cols int) int { - m, n := len(mat), len(mat[0]) - arr := make([]int, m) - for i, row := range mat { - x := 0 - for j, v := range row { - x |= v << j +func maximumRows(matrix [][]int, numSelect int) (ans int) { + m, n := len(matrix), len(matrix[0]) + rows := make([]int, m) + for i, row := range matrix { + for j, x := range row { + if x == 1 { + rows[i] |= 1 << j + } } - arr[i] = x } - ans := 0 - for mask := 1; mask <= 1< b { - return a + if ans < t { + ans = t + } } - return b + return } ``` diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.cpp b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.cpp index c4338e6df0acc..b6678fa1e5fd2 100644 --- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.cpp +++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.cpp @@ -1,18 +1,25 @@ class Solution { public: - int maximumRows(vector>& mat, int cols) { - int m = mat.size(), n = mat[0].size(); - vector arr(m); + int maximumRows(vector>& matrix, int numSelect) { + int m = matrix.size(), n = matrix[0].size(); + int rows[m]; + memset(rows, 0, sizeof(rows)); for (int i = 0; i < m; ++i) { - int x = 0; - for (int j = 0; j < n; ++j) x |= mat[i][j] << j; - arr[i] = x; + for (int j = 0; j < n; ++j) { + if (matrix[i][j]) { + rows[i] |= 1 << j; + } + } } int ans = 0; - for (int mask = 1; mask <= 1 << n; ++mask) { - if (__builtin_popcount(mask) > cols) continue; + for (int mask = 1; mask < 1 << n; ++mask) { + if (__builtin_popcount(mask) != numSelect) { + continue; + } int t = 0; - for (int v : arr) t += (v & mask) == v; + for (int x : rows) { + t += (x & mask) == x; + } ans = max(ans, t); } return ans; diff --git a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.go b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.go index 35879f5bbca12..74789ef60fe16 100644 --- a/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.go +++ b/solution/2300-2399/2397.Maximum Rows Covered by Columns/Solution.go @@ -1,32 +1,26 @@ -func maximumRows(mat [][]int, cols int) int { - m, n := len(mat), len(mat[0]) - arr := make([]int, m) - for i, row := range mat { - x := 0 - for j, v := range row { - x |= v << j +func maximumRows(matrix [][]int, numSelect int) (ans int) { + m, n := len(matrix), len(matrix[0]) + rows := make([]int, m) + for i, row := range matrix { + for j, x := range row { + if x == 1 { + rows[i] |= 1 << j + } } - arr[i] = x } - ans := 0 - for mask := 1; mask <= 1< 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) + +## 题目描述 + + + +

    袋子中装有一些物品,每个物品上都标记着数字 10-1

    + +

    给你四个非负整数 numOnesnumZerosnumNegOnesk

    + +

    袋子最初包含:

    + +
      +
    • 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 1s written on them.
    • +
    • numZeroes items with 0s written on them.
    • +
    • numNegOnes items with -1s 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)