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.

&nbsp;
Example 1:

Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.


Example 2:

Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.


Example 3:

Input: matrix = [[7,8],[1,2]]
Output: [7]
Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.


&nbsp;
Constraints:


	m == mat.length
	n == mat[i].length
	1 &lt;= n, m &lt;= 50
	1 &lt;= matrix[i][j] &lt;= 105.
	All elements in the matrix are distinct.


class Solution:
    def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
        min_, max_ = 0, 0
        min_temp = []
        max_temp = []
        m = len(matrix)
        n = len(matrix[0])
        for i in matrix:
            min_temp.append(min(i))
        print(min_temp)
        if n >= m:
            for i in range(n):
                max_check = []
                for j in range(m):
                    max_check.append(matrix[j][i])
                max_temp.append(max(max_check))
            return set(min_temp).intersection(set(max_temp))
        elif n == 1:
            for i in range(m):
                max_check = []
                for j in range(n):
                    max_check.append(matrix[i][j])
                max_temp.append(max(max_check))
            return [max(max_temp)]
        else:
            for i in range(n):
                max_check = []
                for j in range(m):
                    max_check.append(matrix[j][i])
                max_temp.append(max(max_check))
            return set(min_temp).intersection(set(max_temp))
class Solution {
    public List<Integer> luckyNumbers (int[][] matrix) {
        List<Integer> luckyNums = new ArrayList();
        int n = matrix.length;
        int m = matrix[0].length;
        
        for(int[] row : matrix){
            int min = row[0];
            int index = 0;
            boolean lucky = true;
            for(int col = 0; col < m; col++){
                if(min > row[col]){
                    min = row[col];
                    index = col;
                }
            }
            
            for(int r = 0; r < n; r++){
                if(min < matrix[r][index]){
                    lucky = false;
                    break;
                }
            }
            if(lucky){
                luckyNums.add(min);
            }
            
        }
        
        return luckyNums;
        
    }
}
class Solution {
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {

        unordered_map<int,vector<int>>m;

        for(int i=0;i<matrix.size();i++){
            vector<int>temp = matrix[i];
            for(int j=0;j<temp.size();j++){
                m[j].push_back(temp[j]);
            }
        }

        unordered_map<int,int>mp;
        for(int i=0;i<matrix.size();i++){
            vector<int>helper = matrix[i];

            sort(helper.begin(),helper.end());

            mp[helper[0]]++;
        }
        vector<int>result;
        for(auto i:m){
            vector<int>helper = i.second;
            sort(helper.begin(),helper.end());
            int a = helper[helper.size()-1];
            if(mp.find(a)!=mp.end()){
                result.push_back(a);
            }
        }
        return result;
    }
};
/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var luckyNumbers = function(matrix) {
    let rowLucky = new Set();
    let colLucky = new Set();
    let cols = [...Array(matrix[0].length)].map(e => []);

    for (let i = 0; i < matrix.length; i++) {
        let row = matrix[i];
        rowLucky.add(Math.min(...row));

        // build columns
        for (let j = 0; j < row.length; j++) {
            cols[j].push(row[j]);
        }
    }

    // Compare sets
    for (const col of cols)
        colLucky.add(Math.max(...col));
    return [...rowLucky].filter(x => colLucky.has(x));
};
Lucky Numbers in a Matrix