Solving Leetcode Interviews in Seconds with AI: Most Frequent Prime
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3044" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given a m x n 0-indexed 2D matrix mat. From every cell, you can create numbers in the following way: There could be at most 8 paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east. Select a path from them and append digits in this path to the number being formed by traveling in this direction. 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. Return the most frequent prime number 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. Note: It is invalid to change the direction during the move. Example 1: Input: mat = [[1,1],[9,9],[1,1]] Output: 19 Explanation: From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are: East: [11], South-East: [19], South: [19,191]. Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11]. Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91]. Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91]. Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19]. Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191]. The most frequent prime number among all the created numbers is 19. Example 2: Input: mat = [[7]] Output: -1 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. Example 3: Input: mat = [[9,7,8],[4,6,5],[2,8,6]] Output: 97 Explanation: Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942]. Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79]. Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879]. Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47]. Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68]. Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58]. Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268]. Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85]. Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658]. The most frequent prime number among all the created numbers is 97. Constraints: m == mat.length n == mat[i].length 1 <= m, n <= 6 1 <= mat[i][j] <= 9
Explanation
Here's the breakdown of the solution:
- Traversal and Number Generation: Iterate through each cell of the matrix. For each cell, explore all 8 possible directions (if valid). Construct numbers by appending digits along each direction.
- Prime Number Check and Frequency Tracking: After generating each number, check if it's greater than 10 and if it's a prime number. If both conditions are met, update the frequency of that prime number in a dictionary (or hash map).
Finding Most Frequent Prime: After processing all cells and directions, iterate through the frequency dictionary to find the prime number with the highest frequency. If multiple primes have the same highest frequency, return the largest one.
Runtime Complexity: O(m * n * 8 * L * sqrt(max_number)), where m and n are the dimensions of the matrix, L is the maximum path length (min(m, n)), and max_number is the largest number generated. Storage Complexity: O(P), where P is the number of distinct prime numbers generated.
Code
import math
def is_prime(n):
if n < 2:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def most_frequent_prime(mat):
m = len(mat)
n = len(mat[0])
freq = {}
directions = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
for r in range(m):
for c in range(n):
for dr, dc in directions:
num = 0
curr_r, curr_c = r, c
while 0 <= curr_r < m and 0 <= curr_c < n:
num = num * 10 + mat[curr_r][curr_c]
if num > 10 and is_prime(num):
freq[num] = freq.get(num, 0) + 1
curr_r += dr
curr_c += dc
most_frequent = -1
max_frequency = 0
for num, count in freq.items():
if count > max_frequency:
max_frequency = count
most_frequent = num
elif count == max_frequency and num > most_frequent:
most_frequent = num
return most_frequent