Skip to content

1380. Lucky Numbers in a Matrix #72

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

  1. Identify Row Minimums: For each row, find the minimum element.
  2. Identify Column Maximums: For each column, find the maximum element.
  3. Check for Lucky Numbers: Traverse the matrix and check if an element is both the minimum in its row and the maximum in its column.

Let's implement this solution in PHP: 1380. Lucky Numbers in a Matrix

<?PHP

/**
     * @param Integer[][] $matrix
     * @return Integer[]
     */
    function luckyNumbers ($matrix) {
        $m = count($matrix);
        $n = count($matrix[0]);

        $rowMins = [];
        $colMaxs = array_fill(0, $n, PHP_INT_MIN);

        // Find the minimum in each row
        for ($i = 0; $

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
1 participant