# LEETCODE 1423. Maximum Points You Can Obtain from Cards 解题思路分析

```输入：cardPoints = [1,2,3,4,5,6,1], k = 3

```输入：cardPoints = [2,2,2], k = 2

```输入：cardPoints = [9,7,7,9,7,7,9], k = 7

```输入：cardPoints = [1,1000,1], k = 1

```输入：cardPoints = [1,79,80,1,1,1,200,1], k = 3

• 1 <= cardPoints.length <= 10^5
• 1 <= cardPoints[i] <= 10^4
• 1 <= k <= cardPoints.length

```public int maxScore(int[] cardPoints, int k) {
int score=0; // 前k张牌的总和
for(int i=0;i<k;i++){
score+=cardPoints[i];
}
// 初始时，左边选k张牌，右边选0张牌
int leftIndex=k-1, rightIndex=cardPoints.length;
// 最大得分
int max=score;
// 循环，不断从左边减少一张牌，右边增加一张牌
while(leftIndex>=0){
// 减去左指针这张牌的点数
score-=cardPoints[leftIndex];
// 左指针减一
leftIndex--;
// 右指针减一
rightIndex--;
// 加上右指针这张牌的点数
score+=cardPoints[rightIndex];
// 更新最大点数和
max=Math.max(max, score);
}
return max;
}```

Runtime: 1 ms, faster than 100.00% of Java online submissions for Maximum Points You Can Obtain from Cards.

Memory Usage: 48.2 MB, less than 100.00% of Java online submissions for Maximum Points You Can Obtain from Cards.