Skip to main content
Back to LeetCode Archive
Jul 19, 20245 min read

1380. Lucky Numbers in a Matrix (19 July 2024)

ArrayMatrix

Problem Description

The problem is defined as follows:

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 the maximum in its column.

You can find the problem statement on LeetCode.

Solution Explanation

To solve this problem, we need to find numbers in the matrix that are the minimum in their respective rows and the maximum in their respective columns. Here's a step-by-step explanation of how we can achieve this:

  1. Initialize Required Variables:

    • m: Number of rows in the matrix.
    • n: Number of columns in the matrix.
    • minElems: A vector to store the minimum elements and their column indices for each row.
    • maxElems: A vector to store the maximum elements for each column.
  2. Find the Minimum Elements in Each Row:

    • For each row, find the minimum element and its column index. Store this information in the minElems vector.
  3. Find the Maximum Elements in Each Column:

    • For each column, find the maximum element and store it in the maxElems vector.
  4. Identify Lucky Numbers:

    • For each minimum element found in the rows, check if it is the maximum element in its column. If it is, add it to the result.
  5. Return the Result:

    • The final result is a list of all lucky numbers found in the matrix.

Here is the C++ code implementing this solution:

class Solution {
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        int m = matrix.size();
        int n;
        if(m > 0) n = matrix[0].size();
        else return {};
 
        vector<pair<int,int>> minElems;
        vector<int> maxElems;
 
        for(int i = 0; i < m; i++) {
            int minElem = INT_MAX;
            int col = -1;
            for(int j = 0; j < n; j++) {
                if(matrix[i][j] <= minElem) {
                    minElem = matrix[i][j];
                    col = j;
                }
            }
            minElems.push_back({minElem, col});
        }
        for(int i = 0; i < n; i++) {
            int maxElem = INT_MIN;
            for(int j = 0; j < m; j++) {
                maxElem = max(maxElem, matrix[j][i]);
            }
            maxElems.push_back(maxElem);
        }
 
        vector<int> result;
        for(int i = 0; i < m; i++) {
            if(minElems[i].first == maxElems[minElems[i].second]) {
                result.push_back(minElems[i].first);
            }
        }
        return result;
    }
};

Time and Space Complexities

Time Complexity: O(m * n)

We traverse each element of the matrix twice: once to find the minimum elements in each row and once to find the maximum elements in each column.

Space Complexity: O(m + n)

We use extra space to store the minimum elements of each row and the maximum elements of each column. Specifically, minElems stores m elements, and maxElems stores n elements.

Conclusion

This approach ensures that we efficiently find all the lucky numbers in the matrix by making use of two separate passes: one to find the minimum elements in each row and another to find the maximum elements in each column. This solution has a time complexity of O(m * n), making it efficient for large matrices.

I hope this explanation helps you understand the solution to the problem "Lucky Numbers in a Matrix."