Skip to content

Commit 5381db8

Browse files
committed
Add problem 3212: Count Submatrices With Equal Frequency of X and Y
1 parent fe39106 commit 5381db8

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,7 @@ pub mod problem_3206_alternating_groups_i;
21392139
pub mod problem_3208_alternating_groups_ii;
21402140
pub mod problem_3210_find_the_encrypted_string;
21412141
pub mod problem_3211_generate_binary_strings_without_adjacent_zeros;
2142+
pub mod problem_3212_count_submatrices_with_equal_frequency_of_x_and_y;
21422143
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21432144

21442145
#[cfg(test)]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn number_of_submatrices(grid: Vec<Vec<char>>) -> i32 {
7+
const X: u32 = 'X' as _;
8+
const Y: u32 = 'Y' as _;
9+
10+
let mut iter = grid
11+
.into_iter()
12+
.map(|row| row.into_iter().map(u32::from).collect::<Vec<_>>());
13+
14+
iter.next().map_or(0, |mut cache| {
15+
let mut result = 0;
16+
let mut total_x = 0;
17+
let mut total_y = 0;
18+
19+
for state in &mut cache {
20+
*state = match *state {
21+
X => 1 << 16,
22+
Y => 1,
23+
_ => 0,
24+
};
25+
26+
total_x += *state >> 16;
27+
total_y += *state & 0x_ffff;
28+
result += i32::from(total_x == total_y && total_x != 0);
29+
}
30+
31+
for row in iter {
32+
total_x = 0;
33+
total_y = 0;
34+
35+
cache.iter_mut().zip(row).for_each(|(state, value)| {
36+
*state += match value {
37+
X => 1 << 16,
38+
Y => 1,
39+
_ => 0,
40+
};
41+
42+
total_x += *state >> 16;
43+
total_y += *state & 0x_ffff;
44+
result += i32::from(total_x == total_y && total_x != 0);
45+
});
46+
}
47+
48+
result
49+
})
50+
}
51+
}
52+
53+
// ------------------------------------------------------ snip ------------------------------------------------------ //
54+
55+
impl super::Solution for Solution {
56+
fn number_of_submatrices(grid: Vec<Vec<char>>) -> i32 {
57+
Self::number_of_submatrices(grid)
58+
}
59+
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
#[test]
64+
fn test_solution() {
65+
super::super::tests::run::<super::Solution>();
66+
}
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub mod dynamic_programming;
2+
3+
pub trait Solution {
4+
fn number_of_submatrices(grid: Vec<Vec<char>>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&["XY.", "Y.."] as &[_], 3), (&["XX", "XY"], 0), (&["..", ".."], 0)];
13+
14+
for (grid, expected) in test_cases {
15+
assert_eq!(
16+
S::number_of_submatrices(grid.iter().map(|row| row.chars().collect()).collect()),
17+
expected,
18+
);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)