-
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 2144: Minimum Cost of Buying Candies With Discount
- Loading branch information
Showing
4 changed files
with
101 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
38 changes: 38 additions & 0 deletions
38
src/problem_2144_minimum_cost_of_buying_candies_with_discount/greedy.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,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>(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/problem_2144_minimum_cost_of_buying_candies_with_discount/greedy_2.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,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>(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/problem_2144_minimum_cost_of_buying_candies_with_discount/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,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); | ||
} | ||
} | ||
} |