-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2271: Maximum White Tiles Covered by a Carpet
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/problem_2271_maximum_white_tiles_covered_by_a_carpet/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/problem_2271_maximum_white_tiles_covered_by_a_carpet/sliding_window.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |