当前位置:网站首页>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;
}
边栏推荐
- SqlTransaction
- Operations research note
- 9 excellent bitmap services
- China gaobang brand story: standing guard for safety, gaobang pays attention to
- Graphic system - 1 Layout loading
- golang json 序列化、反序列化 字符串反序列化成 map[string]interface{}
- CORBA 架构体系指南(通用对象请求代理体系架构)
- About the solution of "modulenotfounderror: no module named 'flask.\u compat'"
- 正版ST-link/V2 J-LINK JTAG/SWD引脚定义和注意事项
- Halcon knowledge: matrix topic [01]
猜你喜欢

324. swing sequencing II

独立站卖家如何高效管理复杂的Facebook主页?

IDM certification process log embedding point description

Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference

CORBA 架构体系指南(通用对象请求代理体系架构)

konva系列教程3:自定义图形

Alist+RaiDrive 给电脑整个80亿GB硬盘

如何设计业务高性能高可用计算架构 - 作业

Seata implementation of sharing JDBC distributed transaction

面部識別試驗涉及隱私安全問題?國外一公司被緊急叫停
随机推荐
做跨境电商一定要学会用PRA软件,解放双手提高效率!
Operations research note
EasyCVR新建用户后,视频调阅页面不能点击的问题修复
Openfire 3.8.2集群配置
内存抖动
Database Experiment 7 integrity constraints
IDM certification process log embedding point description
Go 降序排序 取 Top N
EasyExcel 学习笔记
Go, begin, end, for, after, instead of
Cann media data processing V2, jpegd interface introduction
Does face recognition test involve privacy and security issues? A foreign company was urgently stopped
Sword finger offer 11 Minimum number of rotation array
原生实现.NET 5.0+ 自定义日志
Small program graduation project based on wechat examination small program graduation project opening report function reference
Mycat+ sub database and sub table
Konva series tutorial 3: Customizing drawings
Introduction to kubernetes resource object and common commands
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
Huawei cloud AOM released version 2.0, and three features appeared