Skip to content

Commit bdac804

Browse files
committed
sliding window to get max from k on either first sides and return max
1 parent 2079eb1 commit bdac804

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

1423/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// https://dev.to/seanpgallivan/solution-maximum-points-you-can-obtain-from-cards-2no
2+
impl Solution {
3+
pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {
4+
let mut total = card_points.iter().take(k as usize).sum::<i32>();
5+
let mut best = total;
6+
let (mut i, mut j) = (k - 1, card_points.len() - 1);
7+
while i >= 0 {
8+
total += card_points[j] - card_points[i as usize];
9+
best = best.max(total);
10+
i -= 1;
11+
j -= 1;
12+
}
13+
best
14+
}
15+
}

0 commit comments

Comments
 (0)