Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc6d8a8

Browse files
feat: add solutions to lc problem: No.3044 (doocs#2352)
No.3044.Most Frequent Prime
1 parent ba69ef5 commit cc6d8a8

File tree

9 files changed

+627
-5
lines changed

9 files changed

+627
-5
lines changed

‎solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
6262

6363
## 解法
6464

65-
### 方法一
65+
### 方法一:枚举
66+
67+
我们可以枚举所有的下标对 $(i, j),ドル其中 $i \lt j,ドル然后判断 `words[i]` 是否是 `words[j]` 的前缀和后缀,若是则计数加一。
68+
69+
时间复杂度 $O(n^2 \times m),ドル其中 $n$ 和 $m$ 分别为 `words` 的长度和字符串的最大长度。
6670

6771
<!-- tabs:start -->
6872

@@ -145,7 +149,7 @@ function countPrefixSuffixPairs(words: string[]): number {
145149

146150
<!-- tabs:end -->
147151

148-
### 方法二
152+
### 方法二:字典树
149153

150154
<!-- tabs:start -->
151155

‎solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ Therefore, the answer is 0.</pre>
5858

5959
## Solutions
6060

61-
### Solution 1
61+
### Solution 1: Enumeration
62+
63+
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.
64+
65+
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.
6266

6367
<!-- tabs:start -->
6468

‎solution/3000-3099/3044.Most Frequent Prime/README.md‎

Lines changed: 208 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,231 @@
7575

7676
## 解法
7777

78-
### 方法一
78+
### 方法一:哈希表 + 枚举
79+
80+
我们可以使用哈希表来统计每个大于 10 的素数出现的次数。
81+
82+
对于每个单元格,我们可以从它出发,沿着 8 个方向之一生成数字,然后判断生成的数字是否是大于 10ドル$ 的素数,如果是的话,就将它加入到哈希表中。
83+
84+
最后,我们遍历哈希表,找到出现频率最高的素数,如果有多个出现频率最高的素数,那么返回其中最大的那个。
85+
86+
时间复杂度 $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` 的行数和列数。
7987

8088
<!-- tabs:start -->
8189

8290
```python
91+
class Solution:
92+
def mostFrequentPrime(self, mat: List[List[int]]) -> int:
93+
def is_prime(x: int) -> int:
94+
return all(x % i != 0 for i in range(2, isqrt(x) + 1))
8395

96+
m, n = len(mat), len(mat[0])
97+
cnt = Counter()
98+
for i in range(m):
99+
for j in range(n):
100+
for a in range(-1, 2):
101+
for b in range(-1, 2):
102+
if a == 0 and b == 0:
103+
continue
104+
x, y, v = i + a, j + b, mat[i][j]
105+
while 0 <= x < m and 0 <= y < n:
106+
v = v * 10 + mat[x][y]
107+
if is_prime(v):
108+
cnt[v] += 1
109+
x, y = x + a, y + b
110+
ans, mx = -1, 0
111+
for v, x in cnt.items():
112+
if mx < x:
113+
mx = x
114+
ans = v
115+
elif mx == x:
116+
ans = max(ans, v)
117+
return ans
84118
```
85119

86120
```java
121+
class Solution {
122+
public int mostFrequentPrime(int[][] mat) {
123+
int m = mat.length, n = mat[0].length;
124+
Map<Integer, Integer> cnt = new HashMap<>();
125+
for (int i = 0; i < m; ++i) {
126+
for (int j = 0; j < n; ++j) {
127+
for (int a = -1; a <= 1; ++a) {
128+
for (int b = -1; b <= 1; ++b) {
129+
if (a == 0 && b == 0) {
130+
continue;
131+
}
132+
int x = i + a, y = j + b, v = mat[i][j];
133+
while (x >= 0 && x < m && y >= 0 && y < n) {
134+
v = v * 10 + mat[x][y];
135+
if (isPrime(v)) {
136+
cnt.merge(v, 1, Integer::sum);
137+
}
138+
x += a;
139+
y += b;
140+
}
141+
}
142+
}
143+
}
144+
}
145+
int ans = -1, mx = 0;
146+
for (var e : cnt.entrySet()) {
147+
int v = e.getKey(), x = e.getValue();
148+
if (mx < x || (mx == x && ans < v)) {
149+
mx = x;
150+
ans = v;
151+
}
152+
}
153+
return ans;
154+
}
87155

156+
private boolean isPrime(int n) {
157+
for (int i = 2; i <= n / i; ++i) {
158+
if (n % i == 0) {
159+
return false;
160+
}
161+
}
162+
return true;
163+
}
164+
}
88165
```
89166

90167
```cpp
168+
class Solution {
169+
public:
170+
int mostFrequentPrime(vector<vector<int>>& mat) {
171+
int m = mat.size(), n = mat[0].size();
172+
unordered_map<int, int> cnt;
173+
for (int i = 0; i < m; ++i) {
174+
for (int j = 0; j < n; ++j) {
175+
for (int a = -1; a <= 1; ++a) {
176+
for (int b = -1; b <= 1; ++b) {
177+
if (a == 0 && b == 0) {
178+
continue;
179+
}
180+
int x = i + a, y = j + b, v = mat[i][j];
181+
while (x >= 0 && x < m && y >= 0 && y < n) {
182+
v = v * 10 + mat[x][y];
183+
if (isPrime(v)) {
184+
cnt[v]++;
185+
}
186+
x += a;
187+
y += b;
188+
}
189+
}
190+
}
191+
}
192+
}
193+
int ans = -1, mx = 0;
194+
for (auto& [v, x] : cnt) {
195+
if (mx < x || (mx == x && ans < v)) {
196+
mx = x;
197+
ans = v;
198+
}
199+
}
200+
return ans;
201+
}
91202

203+
private:
204+
bool isPrime(int n) {
205+
for (int i = 2; i <= n / i; ++i) {
206+
if (n % i == 0) {
207+
return false;
208+
}
209+
}
210+
return true;
211+
}
212+
};
92213
```
93214
94215
```go
216+
func mostFrequentPrime(mat [][]int) int {
217+
m, n := len(mat), len(mat[0])
218+
cnt := make(map[int]int)
219+
for i := 0; i < m; i++ {
220+
for j := 0; j < n; j++ {
221+
for a := -1; a <= 1; a++ {
222+
for b := -1; b <= 1; b++ {
223+
if a == 0 && b == 0 {
224+
continue
225+
}
226+
x, y, v := i+a, j+b, mat[i][j]
227+
for x >= 0 && x < m && y >= 0 && y < n {
228+
v = v*10 + mat[x][y]
229+
if isPrime(v) {
230+
cnt[v]++
231+
}
232+
x += a
233+
y += b
234+
}
235+
}
236+
}
237+
}
238+
}
239+
ans, mx := -1, 0
240+
for v, x := range cnt {
241+
if mx < x || (mx == x && ans < v) {
242+
mx = x
243+
ans = v
244+
}
245+
}
246+
return ans
247+
}
248+
249+
func isPrime(n int) bool {
250+
for i := 2; i <= n/i; i++ {
251+
if n%i == 0 {
252+
return false
253+
}
254+
}
255+
return true
256+
}
257+
```
258+
259+
```ts
260+
function mostFrequentPrime(mat: number[][]): number {
261+
const m: number = mat.length;
262+
const n: number = mat[0].length;
263+
const cnt: Map<number, number> = new Map();
264+
const isPrime = (x: number): boolean => {
265+
for (let i = 2; i <= x / i; ++i) {
266+
if (x % i === 0) {
267+
return false;
268+
}
269+
}
270+
return true;
271+
};
272+
273+
for (let i = 0; i < m; ++i) {
274+
for (let j = 0; j < n; ++j) {
275+
for (let a = -1; a <= 1; ++a) {
276+
for (let b = -1; b <= 1; ++b) {
277+
if (a === 0 && b === 0) {
278+
continue;
279+
}
280+
let [x, y, v] = [i + a, j + b, mat[i][j]];
281+
while (x >= 0 && x < m && y >= 0 && y < n) {
282+
v = v * 10 + mat[x][y];
283+
if (isPrime(v)) {
284+
cnt.set(v, (cnt.get(v) || 0) + 1);
285+
}
286+
x += a;
287+
y += b;
288+
}
289+
}
290+
}
291+
}
292+
}
95293

294+
let [ans, mx] = [-1, 0];
295+
cnt.forEach((x, v) => {
296+
if (mx < x || (mx === x && ans < v)) {
297+
mx = x;
298+
ans = v;
299+
}
300+
});
301+
return ans;
302+
}
96303
```
97304

98305
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /