Skip to content

Commit 9372aad

Browse files
committed
Add problem 3142: Check if Grid Satisfies Conditions
1 parent 7975429 commit 9372aad

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,7 @@ pub mod problem_3133_minimum_array_end;
21032103
pub mod problem_3136_valid_word;
21042104
pub mod problem_3137_minimum_number_of_operations_to_make_word_k_periodic;
21052105
pub mod problem_3138_minimum_length_of_anagram_concatenation;
2106+
pub mod problem_3142_check_if_grid_satisfies_conditions;
21062107
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21072108

21082109
#[cfg(test)]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn satisfies_conditions(grid: Vec<Vec<i32>>) -> bool {
7+
let (first_row, rest_rows) = grid.split_first().unwrap();
8+
let mut prev = -1;
9+
10+
first_row.iter().all(|&value| {
11+
let result = value != prev;
12+
13+
prev = value;
14+
15+
result
16+
}) && first_row
17+
.iter()
18+
.enumerate()
19+
.all(|(column, &value)| rest_rows.iter().all(|row| row[column] == value))
20+
}
21+
}
22+
23+
// ------------------------------------------------------ snip ------------------------------------------------------ //
24+
25+
impl super::Solution for Solution {
26+
fn satisfies_conditions(grid: Vec<Vec<i32>>) -> bool {
27+
Self::satisfies_conditions(grid)
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
#[test]
34+
fn test_solution() {
35+
super::super::tests::run::<super::Solution>();
36+
}
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn satisfies_conditions(grid: Vec<Vec<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, 0, 2], [1, 0, 2]] as &dyn Matrix<_>, true),
15+
(&[[1, 1, 1], [0, 0, 0]], false),
16+
(&[[1], [2], [3]], false),
17+
];
18+
19+
for (grid, expected) in test_cases {
20+
assert_eq!(S::satisfies_conditions(grid.to_vec()), expected);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)