LC 1380. Lucky Numbers in a Matrix

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
    //finding Transpose :
        vector<vector<int>>res;
        int col_len = matrix[0].size();
        int row_len = matrix.size();
        for(int i=0;i<col_len;i++){
            vector<int>row;
            for(int j=0;j<row_len;j++){
                row.push_back(matrix[j][i]);
            }
            res.push_back(row);
        }
        unordered_map<int,int>umap;
        vector<int> ans;
    // Finding Row Minimum
        for(int i=0;i<m;i++){
            int mn = *min_element(matrix[i].begin(),matrix[i].end());
            for(int j=0;j<n;j++){
                if(matrix[i][j] == mn){
                    umap[matrix[i][j]] = j;
                }
            }
        }
    //Finding Column Maximum
        for(int i=0;i<n;i++){
            int mx = *max_element(res[i].begin(),res[i].end());
            if(umap.find(mx)!= umap.end() && umap[mx] == i){
                ans.push_back(mx);
            }
        }
        return ans;
    }        
};


Dicussion Forum