3044. Most Frequent Prime Medium

@problem@discussion
#Array#Hash Table#Math#Matrix#Counting#Enumeration#Number Theory



1/**
2 * [3044] Most Frequent Prime
3 *
4 * You are given a m x n 0-indexed 2D matrix mat. From every cell, you can create numbers in the following way:
5 * 
6 * 	There could be at most 8 paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east.
7 * 	Select a path from them and append digits in this path to the number being formed by traveling in this direction.
8 * 	Note that numbers are generated at every step, for example, if the digits along the path are 1, 9, 1, then there will be three numbers generated along the way: 1, 19, 191.
9 * 
10 * Return the most frequent <span data-keyword="prime-number">prime number</span> greater than 10 out of all the numbers created by traversing the matrix or -1 if no such prime number exists. If there are multiple prime numbers with the highest frequency, then return the largest among them.
11 * Note: It is invalid to change the direction during the move.
12 *  
13 * <strong class="example">Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2024/02/15/south" style="width: 641px; height: 291px;" /> 
15 * 
16 * Input: mat = [[1,1],[9,9],[1,1]]
17 * Output: 19
18 * Explanation: 
19 * From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are:
20 * East: [11], South-East: [19], South: [19,191].
21 * Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11].
22 * Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91].
23 * Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91].
24 * Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19].
25 * Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191].
26 * The most frequent prime number among all the created numbers is 19.
27 * <strong class="example">Example 2:
28 * 
29 * Input: mat = [[7]]
30 * Output: -1
31 * Explanation: The only number which can be formed is 7. It is a prime number however it is not greater than 10, so return -1.
32 * <strong class="example">Example 3:
33 * 
34 * Input: mat = [[9,7,8],[4,6,5],[2,8,6]]
35 * Output: 97
36 * Explanation: 
37 * Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942].
38 * Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79].
39 * Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879].
40 * Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47].
41 * Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68].
42 * Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58].
43 * Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268].
44 * Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85].
45 * Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658].
46 * The most frequent prime number among all the created numbers is 97.
47 * 
48 *  
49 * Constraints:
50 * 
51 * 	m == mat.length
52 * 	n == mat[i].length
53 * 	1 <= m, n <= 6
54 * 	1 <= mat[i][j] <= 9
55 * 
56 */
57pub struct Solution {}
58
59// problem: https://leetcode.com/problems/most-frequent-prime/
60// discuss: https://leetcode.com/problems/most-frequent-prime/discuss/?currentPage=1&orderBy=most_votes&query=
61
62// submission codes start here
63
64impl Solution {
65    pub fn most_frequent_prime(mat: Vec<Vec<i32>>) -> i32 {
66        0
67    }
68}
69
70// submission codes end
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_3044() {
78    }
79}
80


Back
© 2025 bowen.ge All Rights Reserved.