文章目录
- 题目描述
- 题解思路
- 题解代码
- 题目链接
题目描述
题解思路
题目所求结果存在下述等式
- 可获得的最大点数 = 所有卡牌的点数之和 - 长度为(卡牌数量 - k)的窗口的点数之和的最小值
我们遍历长度为(卡牌数量 - k)的窗口,记录窗口内的所有点数之和的最小值
题解代码
impl Solution {pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {let k = card_points.len() - k as usize;let mut sum = 0;for i in 0..k {sum += card_points[i];}let mut min_win = sum;let mut win = sum;for i in k..card_points.len() {sum += card_points[i];win += card_points[i] - card_points[i-k];min_win = min_win.min(win);}sum - min_win}
}
题目链接
https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/