|
| 1 | +/** |
| 2 | + * [1582] Special Positions in a Binary Matrix |
| 3 | + * |
| 4 | + * Given an m x n binary matrix mat, return the number of special positions in mat. |
| 5 | + * A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed). |
| 6 | + * |
| 7 | + * Example 1: |
| 8 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/special1.jpg" style="width: 244px; height: 245px;" /> |
| 9 | + * Input: mat = [[1,0,0],[0,0,1],[1,0,0]] |
| 10 | + * Output: 1 |
| 11 | + * Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0. |
| 12 | + * |
| 13 | + * Example 2: |
| 14 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/special-grid.jpg" style="width: 244px; height: 245px;" /> |
| 15 | + * Input: mat = [[1,0,0],[0,1,0],[0,0,1]] |
| 16 | + * Output: 3 |
| 17 | + * Explanation: (0, 0), (1, 1) and (2, 2) are special positions. |
| 18 | + * |
| 19 | + * |
| 20 | + * Constraints: |
| 21 | + * |
| 22 | + * m == mat.length |
| 23 | + * n == mat[i].length |
| 24 | + * 1 <= m, n <= 100 |
| 25 | + * mat[i][j] is either 0 or 1. |
| 26 | + * |
| 27 | + */ |
| 28 | +pub struct Solution {} |
| 29 | + |
| 30 | +// problem: https://leetcode.com/problems/special-positions-in-a-binary-matrix/ |
| 31 | +// discuss: https://leetcode.com/problems/special-positions-in-a-binary-matrix/discuss/?currentPage=1&orderBy=most_votes&query= |
| 32 | + |
| 33 | +// submission codes start here |
| 34 | + |
| 35 | +impl Solution { |
| 36 | + pub fn num_special(mat: Vec<Vec<i32>>) -> i32 { |
| 37 | + mat.iter() |
| 38 | + .enumerate() |
| 39 | + .fold( |
| 40 | + ( |
| 41 | + 0, |
| 42 | + mat.iter().enumerate().fold( |
| 43 | + (vec![0; mat.len()], vec![0; mat[0].len()]), |
| 44 | + |(mut row, mut col), (i, v)| { |
| 45 | + v.iter() |
| 46 | + .enumerate() |
| 47 | + .fold((row, col), |(mut row, mut col), (j, &x)| { |
| 48 | + if x == 1 { |
| 49 | + row[i] += 1; |
| 50 | + col[j] += 1; |
| 51 | + } |
| 52 | + (row, col) |
| 53 | + }) |
| 54 | + }, |
| 55 | + ), |
| 56 | + ), |
| 57 | + |(mut c, (row, col)), (i, v)| { |
| 58 | + v.iter().enumerate().for_each(|(j, &x)| { |
| 59 | + if x == 1 && row[i] == 1 && col[j] == 1 { |
| 60 | + c += 1; |
| 61 | + } |
| 62 | + }); |
| 63 | + (c, (row, col)) |
| 64 | + }, |
| 65 | + ) |
| 66 | + .0 |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// submission codes end |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_1582_example_1() { |
| 78 | + let mat = vec![vec![1, 0, 0], vec![0, 0, 1], vec![1, 0, 0]]; |
| 79 | + |
| 80 | + let result = 1; |
| 81 | + |
| 82 | + assert_eq!(Solution::num_special(mat), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_1582_example_2() { |
| 87 | + let mat = vec![vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1]]; |
| 88 | + |
| 89 | + let result = 3; |
| 90 | + |
| 91 | + assert_eq!(Solution::num_special(mat), result); |
| 92 | + } |
| 93 | +} |
0 commit comments