Skip to content

Commit

Permalink
Create 07-mice-and-cheese.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 7, 2023
1 parent d0ca8fe commit 2061d22
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 2023/06/07-mice-and-cheese.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn mice_and_cheese(reward1: Vec<i32>, reward2: Vec<i32>, k: i32) -> i32 {
let mut diff = reward1
.iter()
.zip(reward2.iter())
.map(|(&x, &y)| (x, y, x - y))
.collect::<Vec<_>>();
diff.sort_unstable_by_key(|x| -x.2);
diff.iter().take(k as usize).map(|x| x.0).sum::<i32>()
+ diff.iter().skip(k as usize).map(|x| x.1).sum::<i32>()
}
}

0 comments on commit 2061d22

Please sign in to comment.