Skip to content

Commit

Permalink
Add problem 2271: Maximum White Tiles Covered by a Carpet
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 25, 2024
1 parent 02f7201 commit 327f20d
Show file tree
Hide file tree
Showing 3 changed files with 123 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 @@ -1669,6 +1669,7 @@ pub mod problem_2266_count_number_of_texts;
pub mod problem_2267_check_if_there_is_a_valid_parentheses_string_path;
pub mod problem_2269_find_the_k_beauty_of_a_number;
pub mod problem_2270_number_of_ways_to_split_array;
pub mod problem_2271_maximum_white_tiles_covered_by_a_carpet;
pub mod problem_2273_find_resultant_array_after_removing_anagrams;
pub mod problem_2274_maximum_consecutive_floors_without_special_floors;
pub mod problem_2278_percentage_of_letter_in_string;
Expand Down
24 changes: 24 additions & 0 deletions src/problem_2271_maximum_white_tiles_covered_by_a_carpet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub mod sliding_window;

pub trait Solution {
fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
((&[[1, 5], [10, 11], [12, 18], [20, 25], [30, 32]] as &[_], 10), 9),
((&[[10, 11], [1, 1]], 2), 2),
];

for ((tiles, carpet_len), expected) in test_cases {
assert_eq!(
S::maximum_white_tiles(tiles.iter().map(Vec::from).collect(), carpet_len),
expected,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pub struct Solution;

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

impl Solution {
pub fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
let mut tiles = tiles
.into_iter()
.map(|tile| {
let [start, end]: [_; 2] = tile.as_slice().try_into().ok().unwrap();

(start, end + 1)
})
.collect::<Vec<_>>();

let mut start_index = 0;
let mut start_position = i32::MIN;
let mut covered = 0;
let mut result = 0;

tiles.sort_unstable_by_key(|&(start, _)| start);

for &(right_start, right_end) in &tiles {
let (mut left_start, mut left_end);

loop {
(left_start, left_end) = tiles[start_index];

if start_position < left_start {
if left_start + carpet_len < right_start {
start_position = left_start;
} else {
break;
}
}

if left_end + carpet_len < right_start {
covered -= left_end - start_position;
start_position = left_end;
start_index += 1;
} else {
covered -= right_start - carpet_len - start_position;

break;
}
}

start_position = right_start - carpet_len;

loop {
if start_position < left_start {
if left_start + carpet_len < right_end {
covered += left_start - start_position;
} else {
covered += right_end - carpet_len - start_position;

break;
}
}

if left_end + carpet_len < right_end {
start_position = left_end;
start_index += 1;
(left_start, left_end) = tiles[start_index];
} else {
break;
}
}

if covered == carpet_len {
result = covered;

break;
}

start_position = right_end - carpet_len;
result = result.max(covered);
}

result
}
}

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

impl super::Solution for Solution {
fn maximum_white_tiles(tiles: Vec<Vec<i32>>, carpet_len: i32) -> i32 {
Self::maximum_white_tiles(tiles, carpet_len)
}
}

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

0 comments on commit 327f20d

Please sign in to comment.