Given an m x n matrix mat, where every row is sorted in strictly increasing order, return the smallest common element in all rows. If there is no common element, return -1.
Example 1:
Input: mat = [[1,2,3,4,5],[2,4,5,8,10],[3,5,7,9,11],[1,3,5,7,9]] Output: 5
Example 2:
Input: mat = [[1,2,3],[2,3,4],[2,3,5]] Output: 2
Constraints:
1 <= m, n <= 5001 <= mat[i][j] <= 104mat[i] is sorted in strictly increasing order.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:
We're looking for a number that appears in every single row of a table. The brute force way is to check every possible number to see if it meets this condition. It's like manually checking each student's name on every class roster to see if they're enrolled in all classes.
Here's how the algorithm would work step-by-step:
def find_smallest_common_element(matrix):
first_row = matrix[0]
common_elements = []
for number_to_check in first_row:
is_common = True
# Check if the number is present in all other rows
for row in matrix[1:]:
if number_to_check not in row:
is_common = False
break
# If the number is common, add it to the list
if is_common:
common_elements.append(number_to_check)
# Find the smallest element among common elements
if common_elements:
return min(common_elements)
# If no common element is found, return -1
return -1The most efficient way to find a common element across multiple lists is to leverage the fact that the lists are sorted. We can use a process of elimination by comparing the smallest element in each list and advancing the one that's smallest until we find a common element or exhaust a list.
Here's how the algorithm would work step-by-step:
def find_smallest_common_element(matrix):
row_count = len(matrix)
column_count = len(matrix[0])
element_counts = {}
for row in matrix:
for element in row:
if element in element_counts:
element_counts[element] += 1
else:
element_counts[element] = 1
common_elements = []
# Find numbers that appear in every row
for element, count in element_counts.items():
if count == row_count:
common_elements.append(element)
#Check if there are any common elements
if not common_elements:
return -1
#If there are common elements, find the smallest
smallest_element = common_elements[0]
for element in common_elements:
if element < smallest_element:
smallest_element = element
return smallest_element| Case | How to Handle |
|---|---|
| Empty matrix (mat is null or has zero rows) | Return -1 immediately since there are no rows to find a common element in. |
| Matrix with an empty row (one of mat[i] is null or empty) | Return -1 immediately if any row is empty, as no common element can exist across all rows. |
| Matrix with a single row | Return the first element of the single row if it exists or -1 if the row is empty. |
| Matrix with all rows containing the same element at the beginning | The algorithm should correctly identify that element as the smallest common element. |
| Matrix with duplicate elements within the same row | The algorithm should handle duplicates within rows without incorrectly reporting the smallest common element. |
| No common element exists across all rows | The algorithm should correctly identify that there is no common element and return -1. |
| Integer overflow with extremely large numbers in the matrix | Consider using a data type with a larger range (e.g., long) if integer overflow is a concern, or check for overflow during comparisons. |
| Matrix with very large dimensions (large number of rows and columns) | Ensure the algorithm's time complexity remains reasonable to prevent timeouts; prefer an algorithm with a near-linear time complexity if possible, such as using pointers to track the smallest element in each row. |