Given an m x n
matrix of distinct numbers, return all lucky numbers in the matrix in any order.
A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
Example 1:
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]] Output: [15] Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example 2:
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] Output: [12] Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example 3:
Input: matrix = [[7,8],[1,2]] Output: [7] Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.
Constraints:
m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 105
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach in this matrix problem is all about checking every single number to see if it's a 'lucky number'. We compare each number against all others in its row and column to confirm it meets the lucky number criteria. It's simple but can take a lot of time.
Here's how the algorithm would work step-by-step:
def find_lucky_numbers_brute_force(matrix):
lucky_numbers = []
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
element = matrix[row_index][column_index]
# Find smallest in row
is_smallest_in_row = True
for column_check_index in range(number_of_columns):
if matrix[row_index][column_check_index] < element:
is_smallest_in_row = False
break
# Find largest in column
is_largest_in_column = True
for row_check_index in range(number_of_rows):
if matrix[row_check_index][column_index] > element:
is_largest_in_column = False
break
# Check if both conditions are met
if is_smallest_in_row and is_largest_in_column:
# Found a lucky number
lucky_numbers.append(element)
return lucky_numbers
The goal is to find numbers that are both the smallest in their row and the largest in their column. We can do this efficiently by first finding the smallest number in each row and then checking if any of those are the largest in their respective columns.
Here's how the algorithm would work step-by-step:
def luckyNumbers (matrix):
lucky_numbers = []
# Iterate through each row to find the minimum value
for row in matrix:
minimum_in_row = min(row)
column_index = row.index(minimum_in_row)
# Verify that the minimum in row is the maximum in column
is_largest_in_column = True
for i in range(len(matrix)):
if matrix[i][column_index] > minimum_in_row:
is_largest_in_column = False
break
# Add the lucky number to our list
if is_largest_in_column:
lucky_numbers.append(minimum_in_row)
return lucky_numbers
Case | How to Handle |
---|---|
Null or empty matrix | Return an empty list if the input matrix is null or has zero rows or columns. |
Matrix with only one row or one column | The minimum element of the single row must be the maximum element of its column to be a lucky number, otherwise return empty list. |
Matrix with all identical values | If the minimum value in each row is the same, and that value is the maximum in its column, return that value as a lucky number (might be duplicates). |
Matrix with negative numbers | The algorithm should correctly identify lucky numbers even with negative values present, considering negative minimums and maximums. |
Matrix with extremely large numbers (potential overflow) | Use appropriate data types (e.g., long) to avoid potential integer overflow when comparing numbers. |
Matrix contains duplicate minimum values in a row | Consider all of the duplicate minimum values and check whether the column they are in is maximum for that column. |
No lucky number exists in the matrix | Return an empty list if no element satisfies both the row-minimum and column-maximum conditions. |
Large matrix dimensions (scaling concerns) | The algorithm should scale linearly with the size of the matrix (rows * cols), and avoid operations with higher complexities. |