-
Notifications
You must be signed in to change notification settings - Fork 12
/
1380. Lucky Numbers in a Matrix
36 lines (30 loc) · 1.11 KB
/
1380. Lucky Numbers in a Matrix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
vector<int> ans;
bool a = false , b = false;
int n = matrix.size();
int m = matrix[0].size();
// below loops are for traversing matrix
for(int i=0;i<n;i++){
for(int j = 0;j<m;j++){
// find minimum element in matrix
int at = *min_element(matrix[i].begin(),matrix[i].end());
// check that min element is this element or not
if(matrix[i][j] == at)a = true;
// now check the column
int kt = INT_MIN;
// check for the max element in that column
for(int k = 0;k<n;k++){
if(matrix[k][j]>kt)kt = matrix[k][j];
}
// compare with current element
if(kt == matrix[i][j])b = true;
// if both conditions are true , then store the answer
if(a && b)ans.push_back(matrix[i][j]);
a = false , b = false;
}
}
return ans;
}
};