<p>Given an <code>m x n</code> matrix of <strong>distinct </strong>numbers, return <em>all <strong>lucky numbers</strong> in the matrix in <strong>any </strong>order</em>.</p>

<p>A <strong>lucky number</strong> is an element of the matrix such that it is the minimum element in its row and maximum in its column.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= n, m &lt;= 50</code></li>
	<li><code>1 &lt;= matrix[i][j] &lt;= 10<sup>5</sup></code>.</li>
	<li>All elements in the matrix are distinct.</li>
</ul>

class Solution {
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {
        ios_base::sync_with_stdio(false);cin.tie(0),cout.tie(0);  // Speed up I/O operations
        vector<int> res;  // Vector to store the result
        unordered_map<int,int> row;  // Map to store minimum elements of each row

        // Finding the minimum element in each row
        for(int i = 0; i < matrix.size(); i++) {
            int mini = INT_MAX;  // Initialize mini to the largest integer
            for(int j = 0; j < matrix[0].size(); j++) {
                mini = min(mini, matrix[i][j]);  // Update mini to the minimum value in the row
            }
            row[mini]++;  // Store the minimum value in the map
        }

        // Finding the maximum element in each column
        for(int i = 0; i < matrix[0].size(); i++) {
            int maxi = 0;  // Initialize maxi to zero
            for(int j = 0; j < matrix.size(); j++) {
                maxi = max(maxi, matrix[j][i]);  // Update maxi to the maximum value in the column
            }
            // Check if the maximum value is a lucky number
            if(row.find(maxi) != row.end()) {
                res.push_back(maxi);  // Add the lucky number to the result vector
            }
        }
        
        return res;  // Return the result vector
    }
};
14700