Skip to content

Commit

Permalink
Add problem 2055: Plates Between Candles
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Feb 16, 2024
1 parent 43dc8d3 commit 32068a9
Show file tree
Hide file tree
Showing 3 changed files with 93 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 @@ -1427,6 +1427,7 @@ pub mod problem_2042_check_if_numbers_are_ascending_in_a_sentence;
pub mod problem_2043_simple_bank_system;
pub mod problem_2047_number_of_valid_words_in_a_sentence;
pub mod problem_2053_kth_distinct_string_in_an_array;
pub mod problem_2055_plates_between_candles;
pub mod problem_2057_smallest_index_with_equal_value;
pub mod problem_2058_find_the_minimum_and_maximum_number_of_nodes_between_critical_points;
pub mod problem_2059_minimum_operations_to_convert_number;
Expand Down
27 changes: 27 additions & 0 deletions src/problem_2055_plates_between_candles/mod.rs
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,
);
}
}
}
65 changes: 65 additions & 0 deletions src/problem_2055_plates_between_candles/prefix_sum.rs
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>();
}
}

0 comments on commit 32068a9

Please sign in to comment.