Rust 力扣 - 1423. 可获得的最大点数
题目描述
题解思路
题目所求结果存在下述等式
- 可获得的最大点数 = 所有卡牌的点数之和 - 长度为(卡牌数量 - 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/
原文地址:https://blog.csdn.net/qq_67733273/article/details/143428865
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!