diff --git a/src/lib.rs b/src/lib.rs index 8fd6314f..863f6bc9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/problem_2271_maximum_white_tiles_covered_by_a_carpet/mod.rs b/src/problem_2271_maximum_white_tiles_covered_by_a_carpet/mod.rs new file mode 100644 index 00000000..0f6050d7 --- /dev/null +++ b/src/problem_2271_maximum_white_tiles_covered_by_a_carpet/mod.rs @@ -0,0 +1,24 @@ +pub mod sliding_window; + +pub trait Solution { + fn maximum_white_tiles(tiles: Vec>, carpet_len: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + 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, + ); + } + } +} diff --git a/src/problem_2271_maximum_white_tiles_covered_by_a_carpet/sliding_window.rs b/src/problem_2271_maximum_white_tiles_covered_by_a_carpet/sliding_window.rs new file mode 100644 index 00000000..5f494124 --- /dev/null +++ b/src/problem_2271_maximum_white_tiles_covered_by_a_carpet/sliding_window.rs @@ -0,0 +1,98 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn maximum_white_tiles(tiles: Vec>, 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::>(); + + 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>, carpet_len: i32) -> i32 { + Self::maximum_white_tiles(tiles, carpet_len) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}