-
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 2412: Minimum Money Required Before Transactions
- Loading branch information
Showing
3 changed files
with
61 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
42 changes: 42 additions & 0 deletions
42
src/problem_2412_minimum_money_required_before_transactions/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,42 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 { | ||
let mut max_diff = 0; | ||
let mut diff_sum = 0; | ||
|
||
for transaction in transactions { | ||
let [cost, cashback] = <[_; 2]>::map(transaction.try_into().ok().unwrap(), |x| x as u32); | ||
|
||
let min_diff = if cashback < cost { | ||
diff_sum += u64::from(cost - cashback); | ||
|
||
cashback | ||
} else { | ||
cost | ||
}; | ||
|
||
max_diff = max_diff.max(min_diff); | ||
} | ||
|
||
(diff_sum + u64::from(max_diff)) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 { | ||
Self::minimum_money(transactions) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_2412_minimum_money_required_before_transactions/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,18 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn minimum_money(transactions: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(&[[2, 1], [5, 0], [4, 2]] as &[_], 10), (&[[3, 0], [0, 3]], 3)]; | ||
|
||
for (transactions, expected) in test_cases { | ||
assert_eq!(S::minimum_money(transactions.iter().map(Vec::from).collect()), expected); | ||
} | ||
} | ||
} |