Skip to content

Commit

Permalink
Add problem 2133: Check if Every Row and Column Contains All Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Feb 27, 2024
1 parent ad9667f commit b16a968
Show file tree
Hide file tree
Showing 3 changed files with 1,838 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,7 @@ pub mod problem_2119_a_number_after_a_double_reversal;
pub mod problem_2124_check_if_all_as_appears_before_all_bs;
pub mod problem_2129_capitalize_the_title;
pub mod problem_2130_maximum_twin_sum_of_a_linked_list;
pub mod problem_2133_check_if_every_row_and_column_contains_all_numbers;
pub mod problem_2134_minimum_swaps_to_group_all_1s_together_ii;
pub mod problem_2138_divide_a_string_into_groups_of_size_k;
pub mod problem_2140_solving_questions_with_brainpower;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::ops::{BitOrAssign, Shl};

impl Solution {
fn check<T>(zero: T, one: T, iter: impl IntoIterator<Item = i32>) -> T
where
T: BitOrAssign + Copy + Shl<u32, Output = T>,
{
let mut bits = zero;

for x in iter {
bits |= one << (x as u32 - 1);
}

bits
}

pub fn check_valid(matrix: Vec<Vec<i32>>) -> bool {
let n = matrix.len();

if n < 65 {
let full = if n < 64 { u64::wrapping_shl(1, n as _) } else { 0 }.wrapping_sub(1);

for row in &matrix {
if Self::check(0, 1, row.iter().copied()) != full {
return false;
}
}

for column in 0..n {
if Self::check(0, 1, matrix.iter().map(|row| row[column])) != full {
return false;
}
}
} else {
let full = (1_u128 << n).wrapping_sub(1);

for row in &matrix {
if Self::check(0, 1, row.iter().copied()) != full {
return false;
}
}

for column in 0..n {
if Self::check(0, 1, matrix.iter().map(|row| row[column])) != full {
return false;
}
}
}

true
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn check_valid(matrix: Vec<Vec<i32>>) -> bool {
Self::check_valid(matrix)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Loading

0 comments on commit b16a968

Please sign in to comment.