-
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 1893: Check if All the Integers in a Range Are Covered
- Loading branch information
Showing
3 changed files
with
64 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
39 changes: 39 additions & 0 deletions
39
src/problem_1893_check_if_all_the_integers_in_a_range_are_covered/iterative.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,39 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
fn make_range([start, end]: [i32; 2]) -> u64 { | ||
((1 << end) - 1) ^ ((1 << (start - 1)) - 1) | ||
} | ||
|
||
pub fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool { | ||
let mut covered = 0_u64; | ||
|
||
for range in ranges { | ||
covered |= Self::make_range(range.try_into().ok().unwrap()); | ||
} | ||
|
||
let expected = Self::make_range([left, right]); | ||
|
||
covered & expected == expected | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool { | ||
Self::is_covered(ranges, left, right) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/problem_1893_check_if_all_the_integers_in_a_range_are_covered/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 iterative; | ||
|
||
pub trait Solution { | ||
fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[[1, 2], [3, 4], [5, 6]] as &[_], 2, 5), true), | ||
((&[[1, 10], [10, 20]], 21, 21), false), | ||
]; | ||
|
||
for ((ranges, left, right), expected) in test_cases { | ||
assert_eq!( | ||
S::is_covered(ranges.iter().copied().map(Vec::from).collect(), left, right), | ||
expected, | ||
); | ||
} | ||
} | ||
} |