当前位置:网站首页>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;
}
边栏推荐
- Memory leak
- 百度时间因子添加
- Cannot read property 'MTJ' of undefined
- Qt 中 QObjectCleanupHandler 使用总结
- 好用、强大的PDF 阅读软件综合评测:PDF Expert 、MarginNote、LiquidText、Notability、GoodNotes、Zotero
- idea其他分支合并到dev分支
- 微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
- ONEFLOW source code parsing: automatic inference of operator signature
- 双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
- 180.1.连续登录N天(数据库)
猜你喜欢
随机推荐
百度时间因子添加
Openfire 3.8.2集群配置
AOSP清华镜像下载错误解决
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
Baidu time factor addition
leetcode 1647. Minimum Deletions to Make Character Frequencies Unique(所有字母频率不同的最小删除次数)
use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
新工作第一天
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
Business layer modification - reverse modification based on the existing framework
Analyzing the practical development of robot teaching
Mindspire series one loading image classification data set
tensorboard 使用总结
独立站卖家如何高效管理复杂的Facebook主页?
SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案
ONEFLOW source code parsing: automatic inference of operator signature
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
About Statistical Distributions
获取当前日期0点及23点59时间戳









