-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2352: Equal Row and Column Pairs
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn equal_pairs(grid: Vec<Vec<i32>>) -> i32 { | ||
let mut counts = HashMap::new(); | ||
|
||
for row in &grid { | ||
match counts.entry(&**row) { | ||
Entry::Occupied(occupied_entry) => *occupied_entry.into_mut() += 1, | ||
Entry::Vacant(vacant_entry) => { | ||
vacant_entry.insert(1); | ||
} | ||
} | ||
} | ||
|
||
let mut result = 0; | ||
let n = grid.len(); | ||
let mut buffer = Vec::with_capacity(n); | ||
|
||
for x in 0..n { | ||
buffer.extend(grid.iter().map(|row| row[x])); | ||
|
||
if let Some(count) = counts.get(&*buffer) { | ||
result += count; | ||
} | ||
|
||
buffer.clear(); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn equal_pairs(grid: Vec<Vec<i32>>) -> i32 { | ||
Self::equal_pairs(grid) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pub mod hash_map; | ||
|
||
pub trait Solution { | ||
fn equal_pairs(grid: Vec<Vec<i32>>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities::Matrix; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[3, 2, 1], [1, 7, 6], [2, 7, 7]] as &dyn Matrix<_>, 1), | ||
(&[[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]], 3), | ||
]; | ||
|
||
for (grid, expected) in test_cases { | ||
assert_eq!(S::equal_pairs(grid.to_vec()), expected); | ||
} | ||
} | ||
} |