From fb4e29b039413a7e75696d7db4996da190a77bd6 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 18 Feb 2024 19:58:33 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3044 No.3044.Most Frequent Prime --- .../README.md | 8 +- .../README_EN.md | 6 +- .../3044.Most Frequent Prime/README.md | 209 +++++++++++++++++- .../3044.Most Frequent Prime/README_EN.md | 209 +++++++++++++++++- .../3044.Most Frequent Prime/Solution.cpp | 45 ++++ .../3044.Most Frequent Prime/Solution.go | 41 ++++ .../3044.Most Frequent Prime/Solution.java | 44 ++++ .../3044.Most Frequent Prime/Solution.py | 27 +++ .../3044.Most Frequent Prime/Solution.ts | 43 ++++ 9 files changed, 627 insertions(+), 5 deletions(-) create mode 100644 solution/3000-3099/3044.Most Frequent Prime/Solution.cpp create mode 100644 solution/3000-3099/3044.Most Frequent Prime/Solution.go create mode 100644 solution/3000-3099/3044.Most Frequent Prime/Solution.java create mode 100644 solution/3000-3099/3044.Most Frequent Prime/Solution.py create mode 100644 solution/3000-3099/3044.Most Frequent Prime/Solution.ts diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md index bb06d2d444921..c107233dc411c 100644 --- a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md @@ -62,7 +62,11 @@ i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。 ## 解法 -### 方法一 +### 方法一:枚举 + +我们可以枚举所有的下标对 $(i, j)$,其中 $i \lt j$,然后判断 `words[i]` 是否是 `words[j]` 的前缀和后缀,若是则计数加一。 + +时间复杂度 $O(n^2 \times m)$,其中 $n$ 和 $m$ 分别为 `words` 的长度和字符串的最大长度。 @@ -145,7 +149,7 @@ function countPrefixSuffixPairs(words: string[]): number { -### 方法二 +### 方法二:字典树 diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md index 548d00fb5a388..da3a1cf2eb532 100644 --- a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md @@ -58,7 +58,11 @@ Therefore, the answer is 0. ## Solutions -### Solution 1 +### Solution 1: Enumeration + +We can enumerate all index pairs $(i, j)$, where $i < j$, and then determine whether `words[i]` is a prefix or suffix of `words[j]`. If it is, we increment the count. + +The time complexity is $O(n^2 \times m)$, where $n$ and $m$ are the length of `words` and the maximum length of the strings, respectively. diff --git a/solution/3000-3099/3044.Most Frequent Prime/README.md b/solution/3000-3099/3044.Most Frequent Prime/README.md index 6e3a8f2678c10..76a522f5ca3ed 100644 --- a/solution/3000-3099/3044.Most Frequent Prime/README.md +++ b/solution/3000-3099/3044.Most Frequent Prime/README.md @@ -75,24 +75,231 @@ ## 解法 -### 方法一 +### 方法一:哈希表 + 枚举 + +我们可以使用哈希表来统计每个大于 10 的素数出现的次数。 + +对于每个单元格,我们可以从它出发,沿着 8 个方向之一生成数字,然后判断生成的数字是否是大于 $10$ 的素数,如果是的话,就将它加入到哈希表中。 + +最后,我们遍历哈希表,找到出现频率最高的素数,如果有多个出现频率最高的素数,那么返回其中最大的那个。 + +时间复杂度 $O(m \times n \times \max(m, n) \times {10}^{\frac{\max(m, n)}{2}})$,空间复杂度 $O(m \times n \times \max(m, n))$。其中 $m$ 和 $n$ 分别是 `mat` 的行数和列数。 ```python +class Solution: + def mostFrequentPrime(self, mat: List[List[int]]) -> int: + def is_prime(x: int) -> int: + return all(x % i != 0 for i in range(2, isqrt(x) + 1)) + m, n = len(mat), len(mat[0]) + cnt = Counter() + for i in range(m): + for j in range(n): + for a in range(-1, 2): + for b in range(-1, 2): + if a == 0 and b == 0: + continue + x, y, v = i + a, j + b, mat[i][j] + while 0 <= x < m and 0 <= y < n: + v = v * 10 + mat[x][y] + if is_prime(v): + cnt[v] += 1 + x, y = x + a, y + b + ans, mx = -1, 0 + for v, x in cnt.items(): + if mx < x: + mx = x + ans = v + elif mx == x: + ans = max(ans, v) + return ans ``` ```java +class Solution { + public int mostFrequentPrime(int[][] mat) { + int m = mat.length, n = mat[0].length; + Map cnt = new HashMap<>(); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int a = -1; a <= 1; ++a) { + for (int b = -1; b <= 1; ++b) { + if (a == 0 && b == 0) { + continue; + } + int x = i + a, y = j + b, v = mat[i][j]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt.merge(v, 1, Integer::sum); + } + x += a; + y += b; + } + } + } + } + } + int ans = -1, mx = 0; + for (var e : cnt.entrySet()) { + int v = e.getKey(), x = e.getValue(); + if (mx < x || (mx == x && ans < v)) { + mx = x; + ans = v; + } + } + return ans; + } + private boolean isPrime(int n) { + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +} ``` ```cpp +class Solution { +public: + int mostFrequentPrime(vector>& mat) { + int m = mat.size(), n = mat[0].size(); + unordered_map cnt; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int a = -1; a <= 1; ++a) { + for (int b = -1; b <= 1; ++b) { + if (a == 0 && b == 0) { + continue; + } + int x = i + a, y = j + b, v = mat[i][j]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt[v]++; + } + x += a; + y += b; + } + } + } + } + } + int ans = -1, mx = 0; + for (auto& [v, x] : cnt) { + if (mx < x || (mx == x && ans < v)) { + mx = x; + ans = v; + } + } + return ans; + } +private: + bool isPrime(int n) { + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +}; ``` ```go +func mostFrequentPrime(mat [][]int) int { + m, n := len(mat), len(mat[0]) + cnt := make(map[int]int) + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + for a := -1; a <= 1; a++ { + for b := -1; b <= 1; b++ { + if a == 0 && b == 0 { + continue + } + x, y, v := i+a, j+b, mat[i][j] + for x >= 0 && x < m && y >= 0 && y < n { + v = v*10 + mat[x][y] + if isPrime(v) { + cnt[v]++ + } + x += a + y += b + } + } + } + } + } + ans, mx := -1, 0 + for v, x := range cnt { + if mx < x || (mx == x && ans < v) { + mx = x + ans = v + } + } + return ans +} + +func isPrime(n int) bool { + for i := 2; i <= n/i; i++ { + if n%i == 0 { + return false + } + } + return true +} +``` + +```ts +function mostFrequentPrime(mat: number[][]): number { + const m: number = mat.length; + const n: number = mat[0].length; + const cnt: Map = new Map(); + const isPrime = (x: number): boolean => { + for (let i = 2; i <= x / i; ++i) { + if (x % i === 0) { + return false; + } + } + return true; + }; + + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + for (let a = -1; a <= 1; ++a) { + for (let b = -1; b <= 1; ++b) { + if (a === 0 && b === 0) { + continue; + } + let [x, y, v] = [i + a, j + b, mat[i][j]]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt.set(v, (cnt.get(v) || 0) + 1); + } + x += a; + y += b; + } + } + } + } + } + let [ans, mx] = [-1, 0]; + cnt.forEach((x, v) => { + if (mx < x || (mx === x && ans < v)) { + mx = x; + ans = v; + } + }); + return ans; +} ``` diff --git a/solution/3000-3099/3044.Most Frequent Prime/README_EN.md b/solution/3000-3099/3044.Most Frequent Prime/README_EN.md index 561f6eba582a8..fccd0f254c8bc 100644 --- a/solution/3000-3099/3044.Most Frequent Prime/README_EN.md +++ b/solution/3000-3099/3044.Most Frequent Prime/README_EN.md @@ -71,24 +71,231 @@ The most frequent prime number among all the created numbers is 97. ## Solutions -### Solution 1 +### Solution 1: Hash Table + Enumeration + +We can use a hash table to count the frequency of each prime number greater than 10. + +For each cell, we can start from it, generate a number along one of the 8 directions, and then determine whether the generated number is a prime number greater than 10. If it is, we add it to the hash table. + +Finally, we traverse the hash table to find the prime number with the highest frequency. If there are multiple prime numbers with the highest frequency, we return the largest one. + +The time complexity is $O(m \times n \times \max(m, n) \times {10}^{\frac{\max(m, n)}{2}})$, and the space complexity is $O(m \times n \times \max(m, n))$. Here, $m$ and $n$ are the number of rows and columns of `mat`, respectively. ```python +class Solution: + def mostFrequentPrime(self, mat: List[List[int]]) -> int: + def is_prime(x: int) -> int: + return all(x % i != 0 for i in range(2, isqrt(x) + 1)) + m, n = len(mat), len(mat[0]) + cnt = Counter() + for i in range(m): + for j in range(n): + for a in range(-1, 2): + for b in range(-1, 2): + if a == 0 and b == 0: + continue + x, y, v = i + a, j + b, mat[i][j] + while 0 <= x < m and 0 <= y < n: + v = v * 10 + mat[x][y] + if is_prime(v): + cnt[v] += 1 + x, y = x + a, y + b + ans, mx = -1, 0 + for v, x in cnt.items(): + if mx < x: + mx = x + ans = v + elif mx == x: + ans = max(ans, v) + return ans ``` ```java +class Solution { + public int mostFrequentPrime(int[][] mat) { + int m = mat.length, n = mat[0].length; + Map cnt = new HashMap<>(); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int a = -1; a <= 1; ++a) { + for (int b = -1; b <= 1; ++b) { + if (a == 0 && b == 0) { + continue; + } + int x = i + a, y = j + b, v = mat[i][j]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt.merge(v, 1, Integer::sum); + } + x += a; + y += b; + } + } + } + } + } + int ans = -1, mx = 0; + for (var e : cnt.entrySet()) { + int v = e.getKey(), x = e.getValue(); + if (mx < x || (mx == x && ans < v)) { + mx = x; + ans = v; + } + } + return ans; + } + private boolean isPrime(int n) { + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +} ``` ```cpp +class Solution { +public: + int mostFrequentPrime(vector>& mat) { + int m = mat.size(), n = mat[0].size(); + unordered_map cnt; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int a = -1; a <= 1; ++a) { + for (int b = -1; b <= 1; ++b) { + if (a == 0 && b == 0) { + continue; + } + int x = i + a, y = j + b, v = mat[i][j]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt[v]++; + } + x += a; + y += b; + } + } + } + } + } + int ans = -1, mx = 0; + for (auto& [v, x] : cnt) { + if (mx < x || (mx == x && ans < v)) { + mx = x; + ans = v; + } + } + return ans; + } +private: + bool isPrime(int n) { + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +}; ``` ```go +func mostFrequentPrime(mat [][]int) int { + m, n := len(mat), len(mat[0]) + cnt := make(map[int]int) + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + for a := -1; a <= 1; a++ { + for b := -1; b <= 1; b++ { + if a == 0 && b == 0 { + continue + } + x, y, v := i+a, j+b, mat[i][j] + for x >= 0 && x < m && y >= 0 && y < n { + v = v*10 + mat[x][y] + if isPrime(v) { + cnt[v]++ + } + x += a + y += b + } + } + } + } + } + ans, mx := -1, 0 + for v, x := range cnt { + if mx < x || (mx == x && ans < v) { + mx = x + ans = v + } + } + return ans +} + +func isPrime(n int) bool { + for i := 2; i <= n/i; i++ { + if n%i == 0 { + return false + } + } + return true +} +``` + +```ts +function mostFrequentPrime(mat: number[][]): number { + const m: number = mat.length; + const n: number = mat[0].length; + const cnt: Map = new Map(); + const isPrime = (x: number): boolean => { + for (let i = 2; i <= x / i; ++i) { + if (x % i === 0) { + return false; + } + } + return true; + }; + + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + for (let a = -1; a <= 1; ++a) { + for (let b = -1; b <= 1; ++b) { + if (a === 0 && b === 0) { + continue; + } + let [x, y, v] = [i + a, j + b, mat[i][j]]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt.set(v, (cnt.get(v) || 0) + 1); + } + x += a; + y += b; + } + } + } + } + } + let [ans, mx] = [-1, 0]; + cnt.forEach((x, v) => { + if (mx < x || (mx === x && ans < v)) { + mx = x; + ans = v; + } + }); + return ans; +} ``` diff --git a/solution/3000-3099/3044.Most Frequent Prime/Solution.cpp b/solution/3000-3099/3044.Most Frequent Prime/Solution.cpp new file mode 100644 index 0000000000000..ec82d096568fa --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/Solution.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + int mostFrequentPrime(vector>& mat) { + int m = mat.size(), n = mat[0].size(); + unordered_map cnt; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int a = -1; a <= 1; ++a) { + for (int b = -1; b <= 1; ++b) { + if (a == 0 && b == 0) { + continue; + } + int x = i + a, y = j + b, v = mat[i][j]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt[v]++; + } + x += a; + y += b; + } + } + } + } + } + int ans = -1, mx = 0; + for (auto& [v, x] : cnt) { + if (mx < x || (mx == x && ans < v)) { + mx = x; + ans = v; + } + } + return ans; + } + +private: + bool isPrime(int n) { + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3044.Most Frequent Prime/Solution.go b/solution/3000-3099/3044.Most Frequent Prime/Solution.go new file mode 100644 index 0000000000000..e88d796ec5273 --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/Solution.go @@ -0,0 +1,41 @@ +func mostFrequentPrime(mat [][]int) int { + m, n := len(mat), len(mat[0]) + cnt := make(map[int]int) + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + for a := -1; a <= 1; a++ { + for b := -1; b <= 1; b++ { + if a == 0 && b == 0 { + continue + } + x, y, v := i+a, j+b, mat[i][j] + for x >= 0 && x < m && y >= 0 && y < n { + v = v*10 + mat[x][y] + if isPrime(v) { + cnt[v]++ + } + x += a + y += b + } + } + } + } + } + ans, mx := -1, 0 + for v, x := range cnt { + if mx < x || (mx == x && ans < v) { + mx = x + ans = v + } + } + return ans +} + +func isPrime(n int) bool { + for i := 2; i <= n/i; i++ { + if n%i == 0 { + return false + } + } + return true +} \ No newline at end of file diff --git a/solution/3000-3099/3044.Most Frequent Prime/Solution.java b/solution/3000-3099/3044.Most Frequent Prime/Solution.java new file mode 100644 index 0000000000000..8f133ca576a39 --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/Solution.java @@ -0,0 +1,44 @@ +class Solution { + public int mostFrequentPrime(int[][] mat) { + int m = mat.length, n = mat[0].length; + Map cnt = new HashMap<>(); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int a = -1; a <= 1; ++a) { + for (int b = -1; b <= 1; ++b) { + if (a == 0 && b == 0) { + continue; + } + int x = i + a, y = j + b, v = mat[i][j]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt.merge(v, 1, Integer::sum); + } + x += a; + y += b; + } + } + } + } + } + int ans = -1, mx = 0; + for (var e : cnt.entrySet()) { + int v = e.getKey(), x = e.getValue(); + if (mx < x || (mx == x && ans < v)) { + mx = x; + ans = v; + } + } + return ans; + } + + private boolean isPrime(int n) { + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3044.Most Frequent Prime/Solution.py b/solution/3000-3099/3044.Most Frequent Prime/Solution.py new file mode 100644 index 0000000000000..52e41bc5999ea --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/Solution.py @@ -0,0 +1,27 @@ +class Solution: + def mostFrequentPrime(self, mat: List[List[int]]) -> int: + def is_prime(x: int) -> int: + return all(x % i != 0 for i in range(2, isqrt(x) + 1)) + + m, n = len(mat), len(mat[0]) + cnt = Counter() + for i in range(m): + for j in range(n): + for a in range(-1, 2): + for b in range(-1, 2): + if a == 0 and b == 0: + continue + x, y, v = i + a, j + b, mat[i][j] + while 0 <= x < m and 0 <= y < n: + v = v * 10 + mat[x][y] + if is_prime(v): + cnt[v] += 1 + x, y = x + a, y + b + ans, mx = -1, 0 + for v, x in cnt.items(): + if mx < x: + mx = x + ans = v + elif mx == x: + ans = max(ans, v) + return ans diff --git a/solution/3000-3099/3044.Most Frequent Prime/Solution.ts b/solution/3000-3099/3044.Most Frequent Prime/Solution.ts new file mode 100644 index 0000000000000..03d318d0955dd --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/Solution.ts @@ -0,0 +1,43 @@ +function mostFrequentPrime(mat: number[][]): number { + const m: number = mat.length; + const n: number = mat[0].length; + const cnt: Map = new Map(); + const isPrime = (x: number): boolean => { + for (let i = 2; i <= x / i; ++i) { + if (x % i === 0) { + return false; + } + } + return true; + }; + + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + for (let a = -1; a <= 1; ++a) { + for (let b = -1; b <= 1; ++b) { + if (a === 0 && b === 0) { + continue; + } + let [x, y, v] = [i + a, j + b, mat[i][j]]; + while (x >= 0 && x < m && y >= 0 && y < n) { + v = v * 10 + mat[x][y]; + if (isPrime(v)) { + cnt.set(v, (cnt.get(v) || 0) + 1); + } + x += a; + y += b; + } + } + } + } + } + + let [ans, mx] = [-1, 0]; + cnt.forEach((x, v) => { + if (mx < x || (mx === x && ans < v)) { + mx = x; + ans = v; + } + }); + return ans; +}