Skip to content

Commit

Permalink
Add problem 2326: Spiral Matrix IV
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 20, 2024
1 parent 617d06a commit 98e637b
Show file tree
Hide file tree
Showing 3 changed files with 198 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 @@ -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;
106 changes: 106 additions & 0 deletions src/problem_2326_spiral_matrix_iv/iterative.rs
Original file line number Diff line number Diff line change
@@ -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<Box<ListNode>>) -> Vec<Vec<i32>> {
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<Box<ListNode>>) -> Vec<Vec<i32>> {
Self::spiral_matrix(m, n, head)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
91 changes: 91 additions & 0 deletions src/problem_2326_spiral_matrix_iv/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::data_structures::ListNode;

pub mod iterative;

pub trait Solution {
fn spiral_matrix(m: i32, n: i32, head: Option<Box<ListNode>>) -> Vec<Vec<i32>>;
}

#[cfg(test)]
mod tests {
use super::Solution;
use crate::test_utilities::{self, Matrix};

pub fn run<S: Solution>() {
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,
);
}
}
}

0 comments on commit 98e637b

Please sign in to comment.