Skip to content

Commit

Permalink
Add problem 2144: Minimum Cost of Buying Candies With Discount
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Feb 23, 2024
1 parent 64b9d15 commit 7e72436
Show file tree
Hide file tree
Showing 4 changed files with 101 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 @@ -1457,6 +1457,7 @@ pub mod problem_2130_maximum_twin_sum_of_a_linked_list;
pub mod problem_2134_minimum_swaps_to_group_all_1s_together_ii;
pub mod problem_2138_divide_a_string_into_groups_of_size_k;
pub mod problem_2140_solving_questions_with_brainpower;
pub mod problem_2144_minimum_cost_of_buying_candies_with_discount;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn minimum_cost(cost: Vec<i32>) -> i32 {
let mut cost = cost;

cost.sort_unstable_by(|lhs, rhs| rhs.cmp(lhs));

let mut result = 0;
let mut iter = cost.chunks_exact(3);

for chunk in iter.by_ref() {
result += chunk[0] + chunk[1];
}

result += iter.remainder().iter().sum::<i32>();

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn minimum_cost(cost: Vec<i32>) -> i32 {
Self::minimum_cost(cost)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn minimum_cost(cost: Vec<i32>) -> i32 {
let mut cost = cost;

cost.sort_unstable_by(|lhs, rhs| rhs.cmp(lhs));

let mut result = cost.iter().sum::<i32>();

if let Some(cost) = cost.get(2..) {
for num in cost.iter().step_by(3) {
result -= num;
}
}

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn minimum_cost(cost: Vec<i32>) -> i32 {
Self::minimum_cost(cost)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod greedy;
pub mod greedy_2;

pub trait Solution {
fn minimum_cost(cost: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(&[1, 2, 3] as &[_], 5),
(&[6, 5, 7, 9, 2, 2], 23),
(&[5, 5], 10),
(&[3, 3, 3, 1], 7),
(&[1], 1),
];

for (cost, expected) in test_cases {
assert_eq!(S::minimum_cost(cost.to_vec()), expected);
}
}
}

0 comments on commit 7e72436

Please sign in to comment.