|
1 | | -import math |
2 | | - |
3 | | -class Solution: |
4 | | - |
5 | | - def _is_prime(self, n): |
6 | | - if n <= 1: |
7 | | - return False |
8 | | - for i in range(2, int(math.sqrt(n)) + 1): |
9 | | - if n % i == 0: |
10 | | - return False |
11 | | - return True |
12 | | - |
13 | | - def mostFrequentPrime(self, M: List[List[int]]) -> int: |
14 | | - nums = defaultdict(int) |
15 | | - Y, X = len(M), len(M[0]) |
16 | | - for y in range(Y): |
17 | | - for x in range(X): |
18 | | - for dy, dx in [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]: |
19 | | - y_, x_ = y, x |
20 | | - v = M[y][x] |
21 | | - while Y > y_ + dy >= 0 and X > x_ + dx >= 0: |
22 | | - y_ += dy |
23 | | - x_ += dx |
24 | | - v = v * 10 + M[y_][x_] |
25 | | - if self._is_prime(v): |
26 | | - nums[v] += 1 |
27 | | - |
28 | | - if not nums: |
29 | | - return -1 |
30 | | - |
31 | | - cnt, res = max(nums.values()), 0 |
32 | | - for v, c in nums.items(): |
33 | | - if c == cnt: |
34 | | - res = max(res, v) |
35 | | - |
36 | | - return res |
| 1 | +class Solution(object): |
| 2 | + def mostFrequentPrime(self, mat): |
| 3 | + """ |
| 4 | + :type mat: List[List[int]] |
| 5 | + :rtype: int |
| 6 | + """ |
| 7 | + prime_number_count = {} #key: prime number, value = frequency |
| 8 | + |
| 9 | + directions = [[1, 0], [-1, 0], [0, -1], [0, 1], [1, 1], [1, -1], [-1, 1], [-1, -1]] |
| 10 | + |
| 11 | + row, col = len(mat), len(mat[0]) |
| 12 | + |
| 13 | + def is_prime(number): |
| 14 | + if number < 2: |
| 15 | + return False |
| 16 | + # when I use number/2 instead of number ** 0.5, time limit exceed error happens |
| 17 | + for i in range(2, int(number ** 0.5) + 1): |
| 18 | + if number % i == 0: |
| 19 | + return False |
| 20 | + return True |
| 21 | + |
| 22 | + def travel_path(i, j, dx, dy): |
| 23 | + curr_x, curr_y = i, j |
| 24 | + number = 0 |
| 25 | + curr_number = "" |
| 26 | + while 0<= curr_x < row and 0 <= curr_y < col: |
| 27 | + curr_number += str(mat[curr_x][curr_y]) |
| 28 | + curr_number_int = int(curr_number) |
| 29 | + # number = number * 10 + mat[curr_x][curr_y] |
| 30 | + if len(curr_number) == 1 or curr_number_int == 10: |
| 31 | + curr_x = curr_x + dx |
| 32 | + curr_y = curr_y + dy |
| 33 | + continue |
| 34 | + if is_prime(curr_number_int): |
| 35 | + print(curr_number_int) |
| 36 | + prime_number_count[curr_number_int] = prime_number_count.get(curr_number_int, 0) + 1 |
| 37 | + curr_x = curr_x + dx |
| 38 | + curr_y = curr_y + dy |
| 39 | + # if is_prime(number) and number > 10: |
| 40 | + # prime_number_count[number] = prime_number_count.get(number, 0) + 1 |
| 41 | + # curr_x = curr_x + dx |
| 42 | + # curr_y = curr_y + dy |
| 43 | + |
| 44 | + for i in range(row): |
| 45 | + for j in range(col): |
| 46 | + for dx, dy in directions: |
| 47 | + travel_path(i, j, dx, dy) |
| 48 | + |
| 49 | + |
| 50 | + if not len(prime_number_count): return -1 |
| 51 | + |
| 52 | + res = 0 |
| 53 | + most_frequency = 0 |
| 54 | + |
| 55 | + for key, value in prime_number_count.items(): |
| 56 | + if value > most_frequency: |
| 57 | + res = key |
| 58 | + most_frequency = value |
| 59 | + if value == most_frequency and key > res: |
| 60 | + res = key |
| 61 | + |
| 62 | + return res |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
0 commit comments