Lucky Numbers in a Matrix (LeetCode 1380): Solution and Explanation
Given an m x n matrix of distinct numbers, return every element that is the minimum of its row and the maximum of its column.
Intuition
The obvious method marks every row minimum and every column maximum and reports cells that carry both marks. That works, but a short argument shows the answer has at most one element and leads to a tidier check.
Suppose x at (r1, c1) and y at (r2, c2) were both lucky. Look at the cell z at (r1, c2). x is the minimum of row r1, so x < z. y is the maximum of column c2, so z < y. Hence x < y. Swapping the roles with the cell (r2, c1) gives y < x. Both cannot hold, so there is at most one lucky number.
If a lucky number L exists, every other row minimum is smaller than L: that row's minimum is at most its entry in L's column, which is below L because L is the column maximum. So L is the largest row minimum. By the same argument L is the smallest column maximum.
Conversely, if the largest row minimum equals the smallest column maximum, the two values come from the same cell (values are distinct), which is then lucky. So compare two numbers and you are done.
Approach: Max of row minimums vs. min of column maximums
- Scan every row, take its minimum, and keep the largest one:
rowMinMax. - Scan every column, take its maximum, and keep the smallest one:
colMaxMin. - If
rowMinMax == colMaxMin, return[rowMinMax]. - Otherwise return an empty list.
Step-by-step example
matrix = [[3,7,8],[9,11,13],[15,16,17]]
- Row minimums are 3, 9 and 15. The largest is 15, from row 2.
- Column maximums are 15, 16 and 17. The smallest is 15, from column 0.
- Both equal 15, and that is the cell (2, 0).
- Return [15].
Code: Python, Java, C++
1class Solution:
2 def luckyNumbers(self, matrix: List[List[int]]) -> List[int]:
3 row_min_max = max(min(row) for row in matrix)
4 col_max_min = min(max(col) for col in zip(*matrix))
5 if row_min_max == col_max_min:
6 return [row_min_max]
7 return []1class Solution {
2 public List<Integer> luckyNumbers(int[][] matrix) {
3 int rowMinMax = Integer.MIN_VALUE;
4 for (int[] row : matrix) {
5 int smallest = Integer.MAX_VALUE;
6 for (int v : row) smallest = Math.min(smallest, v);
7 rowMinMax = Math.max(rowMinMax, smallest);
8 }
9 int colMaxMin = Integer.MAX_VALUE;
10 for (int j = 0; j < matrix[0].length; j++) {
11 int largest = Integer.MIN_VALUE;
12 for (int[] row : matrix) largest = Math.max(largest, row[j]);
13 colMaxMin = Math.min(colMaxMin, largest);
14 }
15 List<Integer> result = new ArrayList<>();
16 if (rowMinMax == colMaxMin) result.add(rowMinMax);
17 return result;
18 }
19}1class Solution {
2public:
3 vector<int> luckyNumbers(vector<vector<int>>& matrix) {
4 int rowMinMax = INT_MIN;
5 for (auto& row : matrix) {
6 int smallest = *min_element(row.begin(), row.end());
7 rowMinMax = max(rowMinMax, smallest);
8 }
9 int colMaxMin = INT_MAX;
10 for (size_t j = 0; j < matrix[0].size(); j++) {
11 int largest = INT_MIN;
12 for (auto& row : matrix) largest = max(largest, row[j]);
13 colMaxMin = min(colMaxMin, largest);
14 }
15 if (rowMinMax == colMaxMin) return {rowMinMax};
16 return {};
17 }
18};Time and space complexity
O(m * n)
Each cell is read once while finding row minimums and once while finding column maximums.
O(1)
Only two running values are kept, not arrays of minimums and maximums.
Other approaches
Mark both and intersect (time O(m * n), space O(m + n))
Store the minimum of each row and the maximum of each column in two arrays, then return every cell whose value equals both its row minimum and its column maximum. It is easy to get right and also the same O(m * n) time, with O(m + n) extra space.
Check each row minimum against its column (time O(m * n), space O(1))
For every row, find the minimum and its column, then scan that column to see if it is the maximum there. At most m column scans, so it stays O(m * n).
Common mistakes
- Comparing values without the distinct guarantee in mind. With duplicates the at-most-one argument breaks, but the problem promises distinct values.
- Returning the lucky value as a bare integer instead of a list.
- Mixing up which side takes max and which takes min: it is the maximum of row minimums and the minimum of column maximums.
Frequently asked questions
Why can there be at most one lucky number?
Two lucky cells x and y would force x < y through the cell sharing x's row and y's column, and y < x through the cell sharing y's row and x's column. Both cannot be true.
Why does equality of the two numbers guarantee a lucky cell?
Values are distinct, so the largest row minimum and the smallest column maximum being equal means they are the same cell. That cell is the minimum of its row and the maximum of its column by construction.
Can the answer be empty?
Yes. In [[3,6],[7,1],[5,2],[4,8]] the largest row minimum is 4 but the smallest column maximum is 7, so no cell qualifies.