Skip to content

Commit 0ca0d52

Browse files
committed
Added solution of problem 1582
1 parent 046bec9 commit 0ca0d52

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ pub mod s1560_most_visited_sector_in_a_circular_track;
336336
pub mod s1566_detect_pattern_of_length_m_repeated_k_or_more_times;
337337
pub mod s1572_matrix_diagonal_sum;
338338
pub mod s1576_replace_all_s_to_avoid_consecutive_repeating_characters;
339+
pub mod s1582_special_positions_in_a_binary_matrix;
339340
pub mod s1619_mean_of_array_after_removing_some_elements;
340341
pub mod s1935_maximum_number_of_words_you_can_type;
341342
pub mod s1957_delete_characters_to_make_fancy_string;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
2+
let mut result: i32 = 0;
3+
4+
let mut row_sums: Vec<i32> = vec![0; mat.len()];
5+
let mut col_sums: Vec<i32> = vec![0; mat[0].len()];
6+
7+
for i in 0..mat.len() {
8+
for j in 0..mat[i].len() {
9+
row_sums[i] += mat[i][j];
10+
col_sums[j] += mat[i][j];
11+
}
12+
}
13+
14+
for i in 0..mat.len() {
15+
for j in 0..mat[i].len() {
16+
if mat[i][j] == 1 && row_sums[i] == 1 && col_sums[j] == 1 {
17+
result += 1;
18+
}
19+
}
20+
}
21+
22+
result
23+
}
24+
25+
#[cfg(test)]
26+
mod test {
27+
use super::*;
28+
29+
#[test]
30+
fn test_case_1() {
31+
assert_eq!(
32+
num_special(vec![vec![1, 0, 0], vec![0, 0, 1], vec![1, 0, 0]]),
33+
1
34+
);
35+
}
36+
37+
#[test]
38+
fn test_case_2() {
39+
assert_eq!(
40+
num_special(vec![vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1]]),
41+
3
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)