maximum-points-you-can-obtain-from-cards

This commit is contained in:
Gleb Koval 2022-06-26 16:42:17 +01:00
parent 4a14268528
commit 8976c0171f
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,20 @@
impl Solution {
pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {
let mut sum = card_points.iter()
.take(k as usize)
.sum::<i32>();
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
}
}