#1380
Easy Algorithms Lucky numbers in a matrix
Array Matrix
80.0% acceptance
Feb 25, 2026
2338
123
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 maximum in its column.
Solution
Rust
Time O(n²)
Space O(n)
impl Solution {
pub fn lucky_numbers(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let m = matrix.len();
let n = matrix[0].len();
let row_min: Vec<i32> = matrix.iter().map(|row| *row.iter().min().unwrap()).collect();
let col_max: Vec<i32> = (0..n).map(|c| (0..m).map(|r| matrix[r][c]).max().unwrap()).collect();
let mut result = vec![];
for r in 0..m {
for c in 0..n {
if matrix[r][c] == row_min[r] && matrix[r][c] == col_max[c] {
result.push(matrix[r][c]);
}
}
}
result
}
}