From 9e43d4094bc9b168feed35efe486c5472968ebc3 Mon Sep 17 00:00:00 2001 From: Qiu <99040799+Qiu-IT@users.noreply.github.com> Date: Sun, 26 Mar 2023 07:43:49 +0200 Subject: [PATCH 1/6] feat: add php solution to lc problem: No.0013 (#952) --- .../0000-0099/0013.Roman to Integer/README.md | 22 +++++++++++++++++++ .../0013.Roman to Integer/README_EN.md | 22 +++++++++++++++++++ .../0013.Roman to Integer/Solution.php | 17 ++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 solution/0000-0099/0013.Roman to Integer/Solution.php 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 From 589255efc5ac7a0030ad58799e72bbad98d607bc Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 26 Mar 2023 13:44:45 +0800 Subject: [PATCH 2/6] feat: add solutions to lc problems: No.1735,2600~2603 * No.1735.Count Ways to Make Array With Product * No.2600.K Items With the Maximum Sum * No.2601.Prime Subtraction Operation * No.2602.Minimum Operations to Make All Array Elements Equal * No.2603.Collect Coins in a Tree --- .../0100-0199/0125.Valid Palindrome/README.md | 2 +- .../0125.Valid Palindrome/README_EN.md | 2 +- .../README_EN.md | 2 +- .../README_EN.md | 2 +- .../README.md | 222 ++++++++++++++ .../README_EN.md | 222 ++++++++++++++ .../Solution.cpp | 61 ++++ .../Solution.go | 56 ++++ .../Solution.java | 62 ++++ .../Solution.py | 35 +++ .../README.md | 9 +- .../README_EN.md | 9 +- .../Solutoin.cpp | 8 +- .../README_EN.md | 1 - .../README.md | 144 +++++++++ .../README_EN.md | 125 ++++++++ .../Solution.cpp | 14 + .../Solution.go | 11 + .../Solution.java | 13 + .../Solution.py | 9 + .../README.md | 220 ++++++++++++++ .../README_EN.md | 199 +++++++++++++ .../Solution.cpp | 30 ++ .../Solution.go | 26 ++ .../Solution.java | 42 +++ .../Solution.py | 19 ++ .../README.md | 188 ++++++++++++ .../README_EN.md | 166 +++++++++++ .../Solution.cpp | 20 ++ .../Solution.go | 16 + .../Solution.java | 32 ++ .../Solution.py | 12 + .../2603.Collect Coins in a Tree/README.md | 281 ++++++++++++++++++ .../2603.Collect Coins in a Tree/README_EN.md | 255 ++++++++++++++++ .../2603.Collect Coins in a Tree/Solution.cpp | 51 ++++ .../2603.Collect Coins in a Tree/Solution.go | 51 ++++ .../Solution.java | 50 ++++ .../2603.Collect Coins in a Tree/Solution.py | 22 ++ .../images/graph-2.png | Bin 0 -> 22179 bytes .../images/graph-4.png | Bin 0 -> 26891 bytes solution/CONTEST_README.md | 8 + solution/CONTEST_README_EN.md | 8 + solution/README.md | 4 + solution/README_EN.md | 4 + solution/summary.md | 6 + solution/summary_en.md | 6 + 46 files changed, 2703 insertions(+), 22 deletions(-) create mode 100644 solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.cpp create mode 100644 solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.go create mode 100644 solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.java create mode 100644 solution/1700-1799/1735.Count Ways to Make Array With Product/Solution.py create mode 100644 solution/2600-2699/2600.K Items With the Maximum Sum/README.md create mode 100644 solution/2600-2699/2600.K Items With the Maximum Sum/README_EN.md create mode 100644 solution/2600-2699/2600.K Items With the Maximum Sum/Solution.cpp create mode 100644 solution/2600-2699/2600.K Items With the Maximum Sum/Solution.go create mode 100644 solution/2600-2699/2600.K Items With the Maximum Sum/Solution.java create mode 100644 solution/2600-2699/2600.K Items With the Maximum Sum/Solution.py create mode 100644 solution/2600-2699/2601.Prime Subtraction Operation/README.md create mode 100644 solution/2600-2699/2601.Prime Subtraction Operation/README_EN.md create mode 100644 solution/2600-2699/2601.Prime Subtraction Operation/Solution.cpp create mode 100644 solution/2600-2699/2601.Prime Subtraction Operation/Solution.go create mode 100644 solution/2600-2699/2601.Prime Subtraction Operation/Solution.java create mode 100644 solution/2600-2699/2601.Prime Subtraction Operation/Solution.py create mode 100644 solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README.md create mode 100644 solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/README_EN.md create mode 100644 solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.cpp create mode 100644 solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.go create mode 100644 solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.java create mode 100644 solution/2600-2699/2602.Minimum Operations to Make All Array Elements Equal/Solution.py create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/README.md create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/README_EN.md create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/Solution.cpp create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/Solution.go create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/Solution.java create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/Solution.py create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/images/graph-2.png create mode 100644 solution/2600-2699/2603.Collect Coins in a Tree/images/graph-4.png 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/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..e595a0f72ff40 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 @@ -50,7 +50,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 +92,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/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 0000000000000000000000000000000000000000..abb216183fd03fb6695106654d07ebad8a48f772 GIT binary patch literal 22179 zcmeIa^;=L|7cERFr8G!)DJUh~-7Q_x-7T$jcS#5+-65cqfHczGjWkH-x3=fJ_j$g5 z;okGpIgh~Fd)1s{j5+2GQ&Nz8ib9A20|WC^T1rd>1_loNKO73uWAHB`Kg`v@e_)+e zBt>A#e-ZzNfgy*H788Esp}(JrSgWhn9KzZBOxOzHw-mh$F7q4MxEDxBQioF8qY-at zTri&JARv(l3q2owPcEL$S0wyyTSY8>A=Qy|eQbKTl|Baaz4&_lR(f@uP8RswZgDK>n z-xY#>1pmJutM%2o3Z~LU32e^f)JMH9`M8mYe_#W%6}4Namk+ zJi%6?UpF^H{r5qvV5nZ3I@F|pX0%TCNvUUO0Po)inIO^Q#Lrkgd-2bE^eBS2%uajx zckV)Py%+2Ab&*>wJ0jChJqN>XMedWHYKhah;%|0ew34*z$gp{w@a z9sVz9{THSGOH2RP=Hc6vHm}3xXIWkcjYpdw?So?kuIVEM?=KCDi}F!w23TcN&9gk) z#jxe6OV9x5*gksdat)ylZRzC?=mJPny3X#gGyHBdnjU1MPsT_NWe(GZAwfB!A;}zD$aERZ#13t+E1^`; z^ks^?#nJvEIXqv_8KK|JL(XuUwKS|~K1a#+y|$#;)qET9sJ)jin^;}Pwoc}{e#{AA zroI&mCTKF>!eKNJwzBzF552Tx>`@T zxSE+V@Z*0%#KEZRwO_Y#($7S;n|?9xJmjz%MmL!(+lQ@6|GDX=Tn}J1A#}KGzk5$j z*9ouH0@tE>?IQPaP#a^v?scF9CFi)2 zL$L9TwsJ&*D2Sn=sd`BIpt}E60K4F?AIIQEnGF? z0i0M1Q!NSs!$MwIN(|rQ2!m7ZK`?)&VD@CoOKM`j+hZR$&yJ^@olU6P{9pxls?dKy z_*Q5RY2N2z!LZFA)+gF;!F4LAZc6`qd?Sg^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NjAAu0`Ph literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..c9964a2f1d3a3d333f58a7a91e40bc5d656e0884 GIT binary patch literal 26891 zcmeEu^;=bI*R^anNQfYUfCy}mMo9$$X_1m{1*8O#ZfPk643L!W4(SwBLJ8^a?hq-Z zcOUhB>|#~fqK9i*%%O^AO5|IC>)gt9VsRnMG3!Jkq1xH#}P z5?{IzBYRg|{h9to0$!Dd+UYlM1|E%Ulfe4{p~L~o43C3N zG*$eGr2=>uN`ph0R9M0o2w9})2_szDF49Xf1n4)O9u)racC)E;uXXuR+9h%nzjq+q zbyUS^rjS(rLwKrg&}Mjg%KP+WhznQj42t;q88k*2?L$b6e~0|<|D3_XC6zvt^0^cX z>z@yZ6{F!Fum0yBF{ICMFz_Md%eacqF9l40fAa4~w_d=6;tEVt{`suWG<@+(Ob_ax zw@eda`TO!e?2w@Q=i6WKFr>b`Y1rt$Py0-T!o{|!hz*eVXHdSE#8}W~_Xz$O!*hOk z)THGtf&dsUd>zgFTp73X_2qvDWrK?${W$XUGX8&tbtZ*4p7O@O!<-}*V|i9OaGms@ zaS+F!k)XTH`{3W%f24b^yk(lF!}8BKFt@S%C2UR0kad9GTotBs{HR-*rIU% zZ^Hk#!~b`r|L+b%toi>vXnp>4`p05tu>;8y-|J8IA3m(M&#)@`f>W^&^>Flp(7M5` zr~O5q`y=O+iJyyML4>9`hbxBpfOlQ5ra#ebm6b}=>to$^8~c9J>9JYe+Kor`AK@D< zih?T)LTmY@`MTBit{pF%#giEzZieChoT5A96VI^q=jg}7sd`VVE1O@DkDa6SFi7(La}e8QQ-84N{Ii+-VU}Wo<P#*X=|ayuWE4_U6j1 zwoJu@Rgv1CU#T8;B?=c<3>5}FU=Q~Zy9x=$PTBN4Oz$#^*YvwB^BFY1;gpv&SMYM|=Dt?1y$m2Ae-O#0V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cY)z1nW{P&{!8s*|=H@=T(Yb zb>7^WXvWG4;^$<11(es8g(TxX5lM1?3;C;Fpo~-pArmkfZV)R#`St3Rg5J*x@K~KZzm!SUkJ`El)BdnuFpf@azp`rgqe+T#Z##Zn!Ol9qRRP*r zU481A|2bmq1*%h}5voOZOcLv)Sb`YqX3uf)oGygjqACFmQ+*xbV1o_!v?8h;f#RV+b#{r znS9j;A8ZgwAGL&;xuAw^kmmgOP<${sEyre$y;C1Owz^zthk^#Mv9kkG$ESe=+Uu2AL;0Q@1~HAd^?OkQ#-CBO^9sT}@P>TS24@ z>$de9<R|jJ2m9|tg-+mbNt`&O z|7VgxoC-M9(IC^*#6N=$619Nk?s+;HWbz0%e;54UKm6a4{_h_CZ`1mJwM)mZZ}c>N W9=L;3+6JZ@ZLr5W+tgxwDgOtg2ZPlB literal 0 HcmV?d00001 diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index 2f04243dcc3e7..51d3270a8818e 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -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/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) From 6844d90416f0f0545321d1a480701e9568570eeb Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 26 Mar 2023 16:47:46 +0800 Subject: [PATCH 3/6] feat: add solutions to lc problem: No.1735 No.1735.Count Ways to Make Array With Product --- .../README.md | 16 ++++++++++++++++ solution/CONTEST_README.md | 4 ++-- solution/contest.py | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) 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 e595a0f72ff40..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** diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index 51d3270a8818e..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** 的用户,全站用户名展示为品牌橙色。 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** 的用户,全站用户名展示为品牌橙色。 From e94c71bc95bdc66bb6f906c765dc116935586be0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 26 Mar 2023 17:29:31 +0800 Subject: [PATCH 4/6] feat: add solutions to lc problem: No.1734 No.1734.Decode XORed Permutation --- .../1734.Decode XORed Permutation/README.md | 59 +++++++++++-------- .../README_EN.md | 52 ++++++++-------- .../Solution.cpp | 18 ++++-- .../1734.Decode XORed Permutation/Solution.go | 10 ++-- .../Solution.java | 13 ++-- .../1734.Decode XORed Permutation/Solution.py | 11 ++-- 6 files changed, 93 insertions(+), 70 deletions(-) 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 From 1f5f29dc85904f1b635c25e35fcb655a12586fd5 Mon Sep 17 00:00:00 2001 From: YangFong Date: Sun, 26 Mar 2023 17:35:05 +0800 Subject: [PATCH 5/6] feat: add solutions to lc problem: No.2395 No.2395.Find Subarrays With Equal Sum --- .../README.md | 33 +++++++++++++++++++ .../README_EN.md | 33 +++++++++++++++++++ .../Solution.c | 10 ++++++ .../Solution.rs | 13 ++++++++ 4 files changed, 89 insertions(+) create mode 100644 solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.c create mode 100644 solution/2300-2399/2395.Find Subarrays With Equal Sum/Solution.rs 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 + } +} From 9538a318eace4cb71953e5410a77112ca3204c68 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 26 Mar 2023 18:02:24 +0800 Subject: [PATCH 6/6] feat: add solutions to lc problem: No.2397 No.2397.Maximum Rows Covered by Columns --- .../README.md | 138 ++++++++---------- .../README_EN.md | 130 +++++++---------- .../Solution.cpp | 25 ++-- .../Solution.go | 38 ++--- .../Solution.java | 21 ++- .../Solution.py | 21 ++- 6 files changed, 161 insertions(+), 212 deletions(-) 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