From 8976c0171faf589c17c82da1c2ebbe4bfffae68f Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sun, 26 Jun 2022 16:42:17 +0100 Subject: [PATCH] maximum-points-you-can-obtain-from-cards --- .../sol.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 maximum-points-you-can-obtain-from-cards/sol.rs diff --git a/maximum-points-you-can-obtain-from-cards/sol.rs b/maximum-points-you-can-obtain-from-cards/sol.rs new file mode 100644 index 0000000..c73dc2b --- /dev/null +++ b/maximum-points-you-can-obtain-from-cards/sol.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn max_score(card_points: Vec, k: i32) -> i32 { + let mut sum = card_points.iter() + .take(k as usize) + .sum::(); + let mut max = sum; + for (sub, add) in card_points.iter() + .take(k as usize) + .rev() + .zip( + card_points.iter() + .rev() + .take(k as usize) + ) { + sum += add - sub; + max = max.max(sum); + } + max + } +} \ No newline at end of file