2024-07-19
1380. Lucky Numbers in a Matrix
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.
row 的 min
col 的 max
所以就先每個row 找最小
每個 col 找最大
然後看有沒有相同的
class Solution {
public:
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
vector<int> minV(row, INT_MAX);
vector<int> maxV(col, 0);
// min max
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
int n = matrix[r][c];
if (n < minV[r]) {
minV[r] = n;
}
if (n > maxV[c]) {
maxV[c] = n;
}
}
}
// get the lucky number
vector<int> result;
for (int n : minV) {
if(find(maxV.begin(), maxV.end(), n) != maxV.end()) {
result.push_back(n);
}
}
return result;
}
};