maximum-points-you-can-obtain-from-cards
This commit is contained in:
parent
4a14268528
commit
8976c0171f
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue