class Solution:
def luckyNumbers (self, matrix):
min_in_rows = [min(row) for row in matrix]
max_in_cols = [max(col) for col in zip(*matrix)]
result = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == min_in_rows[i] and matrix[i][j] == max_in_cols[j]:
result.append(matrix[i][j])
return result
class Solution {
public:
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
vector<int> min_in_rows, max_in_cols;
for (auto& row : matrix) {
min_in_rows.push_back(*min_element(row.begin(), row.end()));
}
int cols = matrix[0].size();
for (int j = 0; j < cols; ++j) {
int col_max = matrix[0][j];
for (int i = 1; i < matrix.size(); ++i) {
if (matrix[i][j] > col_max)
col_max = matrix[i][j];
}
max_in_cols.push_back(col_max);
}
vector<int> result;
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == min_in_rows[i] && matrix[i][j] == max_in_cols[j]) {
result.push_back(matrix[i][j]);
}
}
}
return result;
}
};
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int rows = matrix.length, cols = matrix[0].length;
int[] min_in_rows = new int[rows];
int[] max_in_cols = new int[cols];
Arrays.fill(min_in_rows, Integer.MAX_VALUE);
Arrays.fill(max_in_cols, Integer.MIN_VALUE);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
min_in_rows[i] = Math.min(min_in_rows[i], matrix[i][j]);
max_in_cols[j] = Math.max(max_in_cols[j], matrix[i][j]);
}
}
List<Integer> result = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == min_in_rows[i] && matrix[i][j] == max_in_cols[j]) {
result.add(matrix[i][j]);
}
}
}
return result;
}
}
var luckyNumbers = function(matrix) {
let min_in_rows = matrix.map(row => Math.min(...row));
let max_in_cols = [];
for (let j = 0; j < matrix[0].length; j++) {
let colMax = matrix[0][j];
for (let i = 1; i < matrix.length; i++) {
if (matrix[i][j] > colMax)
colMax = matrix[i][j];
}
max_in_cols.push(colMax);
}
let result = [];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] === min_in_rows[i] && matrix[i][j] === max_in_cols[j]) {
result.push(matrix[i][j]);
}
}
}
return result;
};
m x n
matrix of distinct integers called matrix
, a lucky number is an element of the matrix that is the minimum in its row and the maximum in its column. Return all lucky numbers in the matrix in any order.
matrix
(List of Lists or 2D array)O(m + n)
elements (where m
is the number of rows, n
is the number of columns).
To optimize, we can precompute the minimum of each row and the maximum of each column. Then, we only need to check which elements are both the row minimum and the column maximum. This reduces unnecessary repeated work.
We use arrays/lists to store the row minima and column maxima. By iterating through the matrix and comparing, we efficiently find all lucky numbers.
Let's take the following matrix as an example:
[ [3, 7, 8], [9, 11, 13], [15, 16, 17] ]
So the answer is [15]
.
O(mn(m + n))
O(mn)
O(mn)
O(mn)
O(mn)
O(m + n)
for row and column arrays