Skip to content

Commit b42d1d0

Browse files
committed
1582. Special Positions in a Binary Matrix: AC
1 parent 72a221c commit b42d1d0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,3 +1192,4 @@ mod s1576_replace_all_s_to_avoid_consecutive_repeating_characters;
11921192
mod s1577_number_of_ways_where_square_of_number_is_equal_to_product_of_two_numbers;
11931193
mod s1578_minimum_time_to_make_rope_colorful;
11941194
mod s1579_remove_max_number_of_edges_to_keep_graph_fully_traversable;
1195+
mod s1582_special_positions_in_a_binary_matrix;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)