Skip to content

Commit bf09433

Browse files
committed
Add problem 2943: Maximize Area of Square Hole in Grid
1 parent 8108cc3 commit bf09433

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,7 @@ pub mod problem_2937_make_three_strings_equal;
20212021
pub mod problem_2938_separate_black_and_white_balls;
20222022
pub mod problem_2942_find_words_containing_character;
20232023
pub mod problem_2943_maximize_area_of_square_hole_in_grid;
2024+
pub mod problem_2946_matrix_similarity_after_cyclic_shifts;
20242025

20252026
#[cfg(test)]
20262027
mod test_utilities;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool {
7+
mat.iter().all(|row| {
8+
let n = row.len();
9+
10+
row.iter().eq(row.iter().skip(k as u32 as usize % n).chain(row).take(n))
11+
})
12+
}
13+
}
14+
15+
// ------------------------------------------------------ snip ------------------------------------------------------ //
16+
17+
impl super::Solution for Solution {
18+
fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool {
19+
Self::are_similar(mat, k)
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
#[test]
26+
fn test_solution() {
27+
super::super::tests::run::<super::Solution>();
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod brute_force;
2+
3+
pub trait Solution {
4+
fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
use crate::test_utilities::Matrix;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [
14+
((&[[1, 2, 3], [4, 5, 6], [7, 8, 9]] as &dyn Matrix<_>, 4), false),
15+
((&[[1, 2, 1, 2], [5, 5, 5, 5], [6, 3, 6, 3]], 2), true),
16+
((&[[2, 2], [2, 2]], 3), true),
17+
];
18+
19+
for ((mat, k), expected) in test_cases {
20+
assert_eq!(S::are_similar(mat.to_vec(), k), expected);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)