Skip to content

Commit

Permalink
Add problem 1476: Subrectangle Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Dec 16, 2024
1 parent 96dfe01 commit 9c4e6ab
Show file tree
Hide file tree
Showing 4 changed files with 166 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 @@ -562,6 +562,7 @@ pub mod problem_1451_rearrange_words_in_a_sentence;
pub mod problem_1452_people_whose_list_of_favorite_companies_is_not_a_subset_of_another_list;
pub mod problem_1470_shuffle_the_array;
pub mod problem_1472_design_browser_history;
pub mod problem_1476_subrectangle_queries;
pub mod problem_1481_least_number_of_unique_integers_after_k_removals;
pub mod problem_1486_xor_operation_in_an_array;
pub mod problem_1487_making_file_names_unique;
Expand Down
43 changes: 43 additions & 0 deletions src/problem_1476_subrectangle_queries/iterative_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
struct SubrectangleQueries {
rectangle: Vec<Vec<i32>>,
}

impl SubrectangleQueries {
fn new(rectangle: Vec<Vec<i32>>) -> Self {
Self { rectangle }
}

fn update_subrectangle(&mut self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {
for row in row1..=row2 {
for col in col1..=col2 {
self.rectangle[row as usize][col as usize] = new_value;
}
}
}

fn get_value(&self, row: i32, col: i32) -> i32 {
self.rectangle[row as usize][col as usize]
}
}

impl super::SubrectangleQueries for SubrectangleQueries {
fn new(rectangle: Vec<Vec<i32>>) -> Self {
Self::new(rectangle)
}

fn update_subrectangle(&mut self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {
self.update_subrectangle(row1, col1, row2, col2, new_value);
}

fn get_value(&self, row: i32, col: i32) -> i32 {
self.get_value(row, col)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::SubrectangleQueries>();
}
}
48 changes: 48 additions & 0 deletions src/problem_1476_subrectangle_queries/iterative_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
struct SubrectangleQueries {
rectangle: Vec<Vec<i32>>,
operations: Vec<(i32, i32, i32, i32, i32)>,
}

impl SubrectangleQueries {
fn new(rectangle: Vec<Vec<i32>>) -> Self {
Self {
rectangle,
operations: vec![],
}
}

fn update_subrectangle(&mut self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {
self.operations.push((row1, col1, row2, col2, new_value));
}

fn get_value(&self, row: i32, col: i32) -> i32 {
for &(row1, col1, row2, col2, val) in self.operations.iter().rev() {
if row >= row1 && row <= row2 && col >= col1 && col <= col2 {
return val;
}
}
self.rectangle[row as usize][col as usize]
}
}

impl super::SubrectangleQueries for SubrectangleQueries {
fn new(rectangle: Vec<Vec<i32>>) -> Self {
Self::new(rectangle)
}

fn update_subrectangle(&mut self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {
self.update_subrectangle(row1, col1, row2, col2, new_value);
}

fn get_value(&self, row: i32, col: i32) -> i32 {
self.get_value(row, col)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::SubrectangleQueries>();
}
}
74 changes: 74 additions & 0 deletions src/problem_1476_subrectangle_queries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
pub mod iterative_1;
pub mod iterative_2;

pub trait SubrectangleQueries {
fn new(rectangle: Vec<Vec<i32>>) -> Self;
fn update_subrectangle(&mut self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32);
fn get_value(&self, row: i32, col: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::SubrectangleQueries;
use crate::test_utilities::Matrix;

enum Operation {
UpdateSubrectangle(i32, i32, i32, i32, i32),
GetValue((i32, i32), i32),
}

pub fn run<S: SubrectangleQueries>() {
let test_cases = [
(
&[[1, 2, 1], [4, 3, 4], [3, 2, 1], [1, 1, 1]] as &dyn Matrix<_>,
&[
Operation::GetValue((0, 2), 1),
Operation::UpdateSubrectangle(0, 0, 3, 2, 5),
Operation::GetValue((0, 2), 5),
Operation::GetValue((3, 1), 5),
Operation::UpdateSubrectangle(3, 0, 3, 2, 10),
Operation::GetValue((3, 1), 10),
Operation::GetValue((0, 2), 5),
] as &[_],
),
(
&[[1, 1, 1], [2, 2, 2], [3, 3, 3]],
&[
Operation::GetValue((0, 0), 1),
Operation::UpdateSubrectangle(0, 0, 2, 2, 100),
Operation::GetValue((0, 0), 100),
Operation::GetValue((2, 2), 100),
Operation::UpdateSubrectangle(1, 1, 2, 2, 20),
Operation::GetValue((2, 2), 20),
],
),
(
&[[2, 8], [8, 8], [7, 4]],
&[
Operation::GetValue((1, 0), 8),
Operation::UpdateSubrectangle(1, 1, 1, 1, 4),
Operation::GetValue((1, 0), 8),
Operation::GetValue((0, 0), 2),
Operation::UpdateSubrectangle(2, 1, 2, 1, 9),
Operation::GetValue((1, 1), 4),
Operation::GetValue((1, 0), 8),
],
),
];

for (rectangle, operations) in test_cases {
let mut subrectangle_queries = S::new(rectangle.to_vec());

for operation in operations {
match *operation {
Operation::UpdateSubrectangle(row1, col1, row2, col2, new_value) => {
subrectangle_queries.update_subrectangle(row1, col1, row2, col2, new_value);
}
Operation::GetValue((row, col), expected) => {
assert_eq!(subrectangle_queries.get_value(row, col), expected);
}
}
}
}
}
}

0 comments on commit 9c4e6ab

Please sign in to comment.