20250125 finished.
This commit is contained in:
parent
6ac9ec66e8
commit
b2049a210a
|
@ -452,3 +452,5 @@ mod p1561_maximum_number_of_coins_you_can_get;
|
|||
mod p2920_maximum_points_after_collecting_coins_from_all_nodes;
|
||||
|
||||
mod p2944_minimum_number_of_coins_for_fruits;
|
||||
|
||||
mod p2412_minimum_money_required_before_transactions;
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* [2412] Minimum Money Required Before Transactions
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn minimum_money(transactions: Vec<Vec<i32>>) -> i64 {
|
||||
// (total_lose, max(cost, cashback))
|
||||
let (total_lose, mx) = transactions.into_iter().fold((0, 0), |(total_lose, m), v| {
|
||||
let (cost, cash_back) = (v[0] as i64, v[1] as i64);
|
||||
(
|
||||
total_lose + 0.max(cost - cash_back),
|
||||
m.max(cost.min(cash_back)),
|
||||
)
|
||||
});
|
||||
|
||||
total_lose + mx
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2412() {
|
||||
assert_eq!(
|
||||
10,
|
||||
Solution::minimum_money(vec![vec![2, 1], vec![5, 0], vec![4, 2]])
|
||||
);
|
||||
assert_eq!(3, Solution::minimum_money(vec![vec![3, 0], vec![0, 3]]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user