当前位置:网站首页>leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
2022-06-28 18:30:00 【蓝羽飞鸟】
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.
给一组牌,每次只能从头或尾抽一张,一共要抽k张,
把抽到的牌的点数加起来,问能达到的最大点数和是多少。
思路:
容易被抽牌顺序这个概念迷惑,比如是先抽头还是先抽尾呢,
其实可以忽略抽牌顺序这个概念,直接就想:头抽几张,尾抽几张。因为总有顺序能实现。
比如我想要头2张,尾一张,可以通过先头再尾再头这样的顺序实现。
头1张,尾两张,可通过尾,头,尾这样的顺序实现。
于是问题变成了数组头部取几个数字,尾部取几个数字,才能使和最大。
那么用滑动窗口的方法,首先取数组头部k个数字,这是一个窗口,然后依次从窗口右边去掉一个数字,换作数组尾部的一个数字,
直到数组头部0个数字,尾部k个数字为止。
记录下这个过程中最大的和。
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;
}
边栏推荐
- 324. 摆动排序 II
- JQ plug-in analysis
- curl: (56) Recv failure: Connection reset by peer
- use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
- 被315点名的流氓下载器,又回来了…
- 面部识别试验涉及隐私安全问题?国外一公司被紧急叫停
- 堆的概念和代码实现
- Applet graduation project reservation based on wechat housekeeping service applet graduation project opening report function reference
- Is it reliable to open an account for the shares of Oriental Wealth software? Where is safe to open an account
- 解析机器人主持教学的实践发展
猜你喜欢

Record an emotet Trojan horse handling case

Small program graduation design based on wechat real estate intermediary house viewing appointment small program graduation design opening report function reference

PCB线路板布局和布线都有哪些设计要求?

如何高效优雅地管理接口文档

Concept and code implementation of heap

Why can't China produce products like idea?

Unity about oculus quest2 developing 002-ui interaction based on XR interaction Toolkit

亿信华辰:地产企业数字化转型想要把握时代机遇

Small program graduation project based on wechat subscription water supply mall small program graduation project opening report function reference

Konva series tutorial 3: Customizing drawings
随机推荐
被315点名的流氓下载器,又回来了…
用户网络模型与QoE
做跨境电商一定要学会用PRA软件,解放双手提高效率!
双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
Alist+RaiDrive 给电脑整个80亿GB硬盘
CORBA 架构体系指南(通用对象请求代理体系架构)
Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
sqrt()函数的详解和用法「建议收藏」
Advanced C language
剑指 Offer 11. 旋转数组的最小数字
亿信华辰:地产企业数字化转型想要把握时代机遇
实时Transformer:美团在单图像深度估计上的研究
OOM out of memory 内存溢出
打破学科之间壁垒的STEAM教育
Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference
The fourth column, kubernetes cloud native combat, is coming ~
Applet graduation design based on wechat beauty salon technician appointment applet graduation design opening report function reference
WiFi安全漏洞KRACK深度解读
Seata implementation of sharing JDBC distributed transaction
抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱