Skip to content

Latest commit

 

History

History
74 lines (58 loc) · 1.81 KB

_1380. Lucky Numbers in a Matrix.md

File metadata and controls

74 lines (58 loc) · 1.81 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 19, 2024

Last updated : July 19, 2024


Related Topics : Array, Matrix

Acceptance Rate : 79.9 %


Solutions

Python

class Solution:
    def luckyNumbers(self, matrix: List[List[int]]) -> List[int]:
        rowMin = [inf] * len(matrix)
        colMax = [0] * len(matrix[0])

        for i in range(len(matrix)) :
            for j in range(len(matrix[0])) :
                if matrix[i][j] > colMax[j] :
                    colMax[j] = matrix[i][j]
                if matrix[i][j] < rowMin[i] :
                    rowMin[i] = matrix[i][j]

        return list(set(rowMin) & set(colMax))

JavaScript

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var luckyNumbers  = function(matrix) {
    const minRow = new Array(matrix.length);
    const maxCol = new Array(matrix[0].length);
    minRow.fill(Number.MAX_VALUE);
    maxCol.fill(0);

    for (let r = 0; r < matrix.length; r++) {
        for (let c = 0; c < matrix[0].length; c++) {
            if (matrix[r][c] < minRow[r])
                minRow[r] = matrix[r][c];
            if (matrix[r][c] > maxCol[c])
                maxCol[c] = matrix[r][c];
        }
    }

    const output = []
    for (x of minRow) {
        if (maxCol.includes(x))
            output.push(x);
    }

    return output;
};