当前位置:网站首页>leetcode 1423. Maximum points you can obtain from cards
leetcode 1423. Maximum points you can obtain from cards
2022-06-28 18:54:00 【Blue feather bird】
There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Give a set of cards , Only one at a time can be drawn from the beginning or the end , A total of k Zhang ,
Add up the points of the drawn cards , Ask the maximum number of points you can reach .
Ideas :
Easily confused by the concept of draw order , For example, whether to draw the tail first or draw the tail first ,
In fact, the concept of draw order can be ignored , Just think : First draw a few , Draw a few at the end . Because there is always order to achieve .
For example, I want a head 2 Zhang , The last one , It can be realized by the sequence of beginning, ending and beginning .
head 1 Zhang , Last two , You can use the tail , head , The tail is realized in this order .
So the problem becomes how many numbers are taken from the head of the array , Take a few numbers from the tail , To maximize the sum .
Then use the sliding window method , First take the array header k A digital , This is a window , Then remove a number from the right side of the window in turn , Replace it with a number at the end of the array ,
Until the array header 0 A digital , The tail k A number .
Record the largest sum in the process .
public int maxScore(int[] cardPoints, int k) {
int res = 0;
int i = 0;
int sum = 0;
int n = cardPoints.length;
while(i < k) {
sum += cardPoints[i];
i ++;
}
i = k - 1;
res = sum;
while(i >= 0) {
sum = sum - cardPoints[i] + cardPoints[n - k + i];
res = Math.max(res, sum);
i --;
}
return res;
}
边栏推荐
猜你喜欢

Lumiprobe 蛋白质标记研究方案

Qt 中 QObjectCleanupHandler 使用总结

打破学科之间壁垒的STEAM教育

Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known

浦发银行软件测试面试真题(小编面试亲测)

About Statistical Distributions

抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱

About Covariance and Correlation(协方差和相关)

tensorboard 使用总结

Enhancing steam and engineering education from theory to practice
随机推荐
被315点名的流氓下载器,又回来了…
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
Huawei cloud AOM released version 2.0, and three features appeared
声网发布灵隼物联网云平台 可一小时构建示例场景
devpi
深入解析kubernetes中的选举机制
毕业设计-基于Unity的餐厅经营游戏的设计与开发(附源码、开题报告、论文、答辩PPT、演示视频,带数据库)
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
Concept and code implementation of heap
手动备份和还原DHCP服务器
【C#】详解值类型和引用类型区别
C# 41. Int to string
微软独家付费功能,也被完美解锁了
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
devpi
独立站卖家如何高效管理复杂的Facebook主页?
【软件测试】2022年普通高等学校招生全国统一考试
Alist+RaiDrive 给电脑整个80亿GB硬盘
百度时间因子添加
打破学科之间壁垒的STEAM教育