1380. Lucky Numbers in a Matrix

Given a m * 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.

 

Example 1:

Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column

Example 2:

Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.

Example 3:

Input: matrix = [[7,8],[1,2]]
Output: [7]

 

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= n, m <= 50
  • 1 <= matrix[i][j] <= 10^5.
  • All elements in the matrix are distinct.

Rust Solution

struct Solution;

impl Solution {
    fn lucky_numbers(matrix: Vec<Vec<i32>>) -> Vec<i32> {
        let n = matrix.len();
        let m = matrix[0].len();
        let mut mins: Vec<i32> = vec![std::i32::MAX; n];
        let mut maxs: Vec<i32> = vec![std::i32::MIN; m];
        let mut res = vec![];
        for i in 0..n {
            for j in 0..m {
                mins[i] = mins[i].min(matrix[i][j]);
                maxs[j] = maxs[j].max(matrix[i][j]);
            }
        }
        for i in 0..n {
            for j in 0..m {
                if mins[i] == matrix[i][j] && maxs[j] == matrix[i][j] {
                    res.push(matrix[i][j]);
                }
            }
        }
        res
    }
}

#[test]
fn test() {
    let matrix = vec_vec_i32![[3, 7, 8], [9, 11, 13], [15, 16, 17]];
    let res = vec![15];
    assert_eq!(Solution::lucky_numbers(matrix), res);
    let matrix = vec_vec_i32![[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]];
    let res = vec![12];
    assert_eq!(Solution::lucky_numbers(matrix), res);
    let matrix = vec_vec_i32![[7, 8], [1, 2]];
    let res = vec![7];
    assert_eq!(Solution::lucky_numbers(matrix), res);
}

Having problems with this solution? Click here to submit an issue on github.