Skip to content

Create 1380. Lucky Numbers in a Matrix #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 1380. Lucky Numbers in a Matrix
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};
Loading