-
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 2055: Plates Between Candles
- Loading branch information
Showing
3 changed files
with
93 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
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,27 @@ | ||
pub mod prefix_sum; | ||
|
||
pub trait Solution { | ||
fn plates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("**|**|***|", &[[2, 5], [5, 9]] as &[_]), &[2, 3] as &[_]), | ||
( | ||
("***|**|*****|**||**|*", &[[1, 17], [4, 5], [14, 17], [5, 11], [15, 16]]), | ||
&[9, 0, 0, 0, 0], | ||
), | ||
]; | ||
|
||
for ((s, queries), expected) in test_cases { | ||
assert_eq!( | ||
S::plates_between_candles(s.to_string(), queries.iter().map(Vec::from).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |
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,65 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
pub fn plates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> { | ||
let mut prefix_sums = vec![(0_u32, 0_u32); s.len()].into_boxed_slice(); | ||
let mut full_plates = 0; | ||
let mut current_plates = 0; | ||
|
||
for (target, c) in prefix_sums.iter_mut().zip(s.bytes()) { | ||
if c == b'|' { | ||
full_plates += current_plates; | ||
current_plates = 0; | ||
} else { | ||
current_plates += 1; | ||
} | ||
|
||
target.1 = full_plates; | ||
} | ||
|
||
full_plates += current_plates; | ||
current_plates = 0; | ||
|
||
for (target, c) in prefix_sums.iter_mut().zip(s.bytes()).rev() { | ||
if c == b'|' { | ||
full_plates -= current_plates; | ||
current_plates = 0; | ||
} else { | ||
current_plates += 1; | ||
} | ||
|
||
target.0 = full_plates; | ||
} | ||
|
||
queries | ||
.into_iter() | ||
.map(|query| { | ||
let [left, right]: [_; 2] = query.try_into().ok().unwrap(); | ||
let right_sum = prefix_sums[right as u32 as usize].1; | ||
let left_sum = prefix_sums[left as u32 as usize].0; | ||
|
||
right_sum.saturating_sub(left_sum) as _ | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn plates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> { | ||
Self::plates_between_candles(s, queries) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |