From 98e637b7ede85434d154e7ce6c9c47381489e4a5 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Wed, 20 Nov 2024 23:02:03 +0800 Subject: [PATCH] Add problem 2326: Spiral Matrix IV --- src/lib.rs | 1 + .../iterative.rs | 106 ++++++++++++++++++ src/problem_2326_spiral_matrix_iv/mod.rs | 91 +++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 src/problem_2326_spiral_matrix_iv/iterative.rs create mode 100644 src/problem_2326_spiral_matrix_iv/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 21aa9bad..fe7682e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1728,6 +1728,7 @@ pub mod problem_2319_check_if_matrix_is_x_matrix; pub mod problem_2320_count_number_of_ways_to_place_houses; pub mod problem_2321_maximum_score_of_spliced_array; pub mod problem_2325_decode_the_message; +pub mod problem_2326_spiral_matrix_iv; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2326_spiral_matrix_iv/iterative.rs b/src/problem_2326_spiral_matrix_iv/iterative.rs new file mode 100644 index 00000000..75b5708e --- /dev/null +++ b/src/problem_2326_spiral_matrix_iv/iterative.rs @@ -0,0 +1,106 @@ +use crate::data_structures::ListNode; + +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn spiral_matrix(m: i32, n: i32, head: Option>) -> Vec> { + let m = m as u32 as usize; + let n = n as u32 as usize; + let mut result = vec![vec![-1; n]; m]; + let mut top = 0; + let mut bottom = m; + let mut left = 0; + let mut right = n; + let mut node = head; + + 'outer: loop { + // Right. + + for target in &mut result[top][left..right] { + if let Some(boxed_node) = node { + node = boxed_node.next; + *target = boxed_node.val; + } else { + break 'outer; + } + } + + top += 1; + + if top == bottom { + break; + } + + // Down. + + for row in &mut result[top..bottom] { + if let Some(boxed_node) = node { + node = boxed_node.next; + row[right - 1] = boxed_node.val; + } else { + break 'outer; + } + } + + right -= 1; + + if left == right { + break; + } + + // Left. + + for target in &mut result[bottom - 1][left..right].iter_mut().rev() { + if let Some(boxed_node) = node { + node = boxed_node.next; + *target = boxed_node.val; + } else { + break 'outer; + } + } + + bottom -= 1; + + if top == bottom { + break; + } + + // Up. + + for row in &mut result[top..bottom].iter_mut().rev() { + if let Some(boxed_node) = node { + node = boxed_node.next; + row[left] = boxed_node.val; + } else { + break 'outer; + } + } + + left += 1; + + if left == right { + break; + } + } + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn spiral_matrix(m: i32, n: i32, head: Option>) -> Vec> { + Self::spiral_matrix(m, n, head) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2326_spiral_matrix_iv/mod.rs b/src/problem_2326_spiral_matrix_iv/mod.rs new file mode 100644 index 00000000..e6888684 --- /dev/null +++ b/src/problem_2326_spiral_matrix_iv/mod.rs @@ -0,0 +1,91 @@ +use crate::data_structures::ListNode; + +pub mod iterative; + +pub trait Solution { + fn spiral_matrix(m: i32, n: i32, head: Option>) -> Vec>; +} + +#[cfg(test)] +mod tests { + use super::Solution; + use crate::test_utilities::{self, Matrix}; + + pub fn run() { + let test_cases = [ + ( + (3, 5, &[3, 0, 2, 6, 8, 1, 7, 9, 4, 2, 5, 5, 0] as &[_]), + &[[3, 0, 2, 6, 8], [5, 0, -1, -1, 1], [5, 2, 4, 9, 7]] as &dyn Matrix<_>, + ), + ((1, 4, &[0, 1, 2]), &[[0, 1, 2, -1]]), + ((1, 8, &[2, 11, 13, 21, 21, 1, 8, 12]), &[[2, 11, 13, 21, 21, 1, 8, 12]]), + ( + (10, 8, &[483, 100, 904, 632, 267, 352, 386, 887, 753]), + &[ + [483, 100, 904, 632, 267, 352, 386, 887], + [-1, -1, -1, -1, -1, -1, -1, 753], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1, -1, -1], + ], + ), + ( + (10, 1, &[8, 24, 5, 21, 10, 11, 11, 12, 6, 17]), + &[[8], [24], [5], [21], [10], [11], [11], [12], [6], [17]], + ), + ( + ( + 10, + 5, + &[47, 708, 1, 918, 227, 334, 115, 863, 636, 769, 117, 557, 940, 54, 83], + ), + &[ + [47, 708, 1, 918, 227], + [-1, -1, -1, -1, 334], + [-1, -1, -1, -1, 115], + [-1, -1, -1, -1, 863], + [-1, -1, -1, -1, 636], + [-1, -1, -1, -1, 769], + [-1, -1, -1, -1, 117], + [-1, -1, -1, -1, 557], + [-1, -1, -1, -1, 940], + [-1, -1, -1, 83, 54], + ], + ), + ( + ( + 9, + 6, + &[ + 995, 348, 36, 516, 333, 627, 248, 422, 13, 225, 764, 311, 405, 695, 698, 83, 145, 783, 478, + ], + ), + &[ + [995, 348, 36, 516, 333, 627], + [-1, -1, -1, -1, -1, 248], + [-1, -1, -1, -1, -1, 422], + [-1, -1, -1, -1, -1, 13], + [-1, -1, -1, -1, -1, 225], + [-1, -1, -1, -1, -1, 764], + [-1, -1, -1, -1, -1, 311], + [-1, -1, -1, -1, -1, 405], + [478, 783, 145, 83, 698, 695], + ], + ), + ((2, 3, &[1, 2, 3, 4, 5, 6]), &[[1, 2, 3], [6, 5, 4]]), + ((3, 2, &[1, 2, 3, 4, 5, 6]), &[[1, 2], [6, 3], [5, 4]]), + ]; + + for ((m, n, head), expected) in test_cases { + assert_eq!( + S::spiral_matrix(m, n, test_utilities::make_list(head.iter().copied())), + expected, + ); + } + } +}