classSolution: defluckyNumbers(self, matrix: List[List[int]]) -> List[int]: ans = [] for row in matrix: for j, x inenumerate(row): ifmax(r[j] for r in matrix) <= x <= min(row): ans.append(x) return ans
publicclassSolution { public IList<int> LuckyNumbers (int[][] matrix) { int m = matrix.Length, n = matrix[0].Length; IList<int> ret = new List<int>(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { bool isMin = true, isMax = true; for (int k = 0; k < n; k++) { if (matrix[i][k] < matrix[i][j]) { isMin = false; break; } } if (!isMin) { continue; } for (int k = 0; k < m; k++) { if (matrix[k][j] > matrix[i][j]) { isMax = false; break; } } if (isMax) { ret.Add(matrix[i][j]); } } } return ret; } }
var luckyNumbers = function(matrix) { const m = matrix.length, n = matrix[0].length; const ret = []; for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { let isMin = true, isMax = true; for (let k = 0; k < n; k++) { if (matrix[i][k] < matrix[i][j]) { isMin = false; break; } } if (!isMin) { continue; } for (let k = 0; k < m; k++) { if (matrix[k][j] > matrix[i][j]) { isMax = false; break; } } if (isMax) { ret.push(matrix[i][j]); } } } return ret; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcluckyNumbers(matrix [][]int) (ans []int) { for _, row := range matrix { next: for j, x := range row { for _, y := range row { if y < x { continue next } } for _, r := range matrix { if r[j] > x { continue next } } ans = append(ans, x) } } return }
复杂度分析
时间复杂度:O(mn \times (m + n)),其中 m 和 n 分别是矩阵 matrix 的行数和列数。遍历矩阵 matrix 需要 O(mn),查找行最小值需要 O(n),查找列最大值需要 O(m)。
classSolution: defluckyNumbers(self, matrix: List[List[int]]) -> List[int]: minRow = [min(row) for row in matrix] maxCol = [max(col) for col inzip(*matrix)] ans = [] for i, row inenumerate(matrix): for j, x inenumerate(row): if x == minRow[i] == maxCol[j]: ans.append(x) return ans
funcluckyNumbers(matrix [][]int) (ans []int) { minRow := make([]int, len(matrix)) maxCol := make([]int, len(matrix[0])) for i, row := range matrix { minRow[i] = row[0] for j, x := range row { minRow[i] = min(minRow[i], x) maxCol[j] = max(maxCol[j], x) } } for i, row := range matrix { for j, x := range row { if x == minRow[i] && x == maxCol[j] { ans = append(ans, x) } } } return }
funcmin(a, b int)int { if a > b { return b } return a }
funcmax(a, b int)int { if b > a { return b } return a }
复杂度分析
时间复杂度:O(mn),其中 m 和 n 分别是矩阵 matrix 的行数和列数。预处理 minRow 和 maxCol 需要 O(mn),查找幸运数需要 O(mn)。
classSolution { public: vector<int> luckyNumbers(vector<vector<int>>& matrix){ int m = matrix.size(); int rowMax = 0, k; for (int i = 0; i < m; i++) { int c = min_element(matrix[i].begin(), matrix[i].end()) - matrix[i].begin(); if (rowMax < matrix[i][c]) { rowMax = matrix[i][c]; k = c; } } for (int i = 0; i < m; i++) { if (rowMax < matrix[i][k]) { return {}; } } return {rowMax}; } };
publicclassSolution { public IList<int> LuckyNumbers (int[][] matrix) { IList<int> ret = new List<int>(); int m = matrix.Length, n = matrix[0].Length; int rowMax = 0, k = 0; for (int i = 0; i < m; i++) { int curMin = matrix[i][0]; int c = 0; for (int j = 1; j < n; j++) { if (curMin > matrix[i][j]) { curMin = matrix[i][j]; c = j; } } if (rowMax < curMin) { rowMax = curMin; k = c; } } for (int i = 0; i < m; i++) { if (rowMax < matrix[i][k]) { return ret; } } ret.Add(rowMax); return ret; } }
int *luckyNumbers(int **matrix, int matrixSize, int *matrixColSize, int *returnSize){ int m = matrixSize, n = matrixColSize[0]; int rowMax = 0, k; for (int i = 0; i < m; i++) { int index = 0; for (int j = 1; j < n; j++) { if (matrix[i][index] > matrix[i][j]) { index = j; } } if (rowMax < matrix[i][index]) { rowMax = matrix[i][index]; k = index; } }
for (int i = 0; i < m; i++) { if (rowMax < matrix[i][k]) { *returnSize = 0; returnNULL; } }
funcluckyNumbers(matrix [][]int) []int { rowMax, k := 0, 0 for _, row := range matrix { c := 0 for j, v := range row { if v < row[c] { c = j } } if row[c] > rowMax { rowMax = row[c] k = c } } for _, row := range matrix { if row[k] > rowMax { returnnil } } return []int{rowMax} }
var luckyNumbers = function(matrix) { const ret = []; const m = matrix.length, n = matrix[0].length; let rowMax = 0, k = 0; for (let i = 0; i < m; i++) { let curMin = matrix[i][0]; let c = 0; for (let j = 1; j < n; j++) { if (curMin > matrix[i][j]) { curMin = matrix[i][j]; c = j; } } if (rowMax < curMin) { rowMax = curMin; k = c; } } for (let i = 0; i < m; i++) { if (rowMax < matrix[i][k]) { return ret; } } ret.push(rowMax); return ret; };
复杂度分析
时间复杂度:O(mn),其中 m 和 n 分别是矩阵 matrix 的行数和列数。获取 rowMax 需要 O(mn),判断 rowMax 是否是列最大值需要 O(m)。