当前位置:网站首页>Leetcode 1423.可获得的最大点数(Maximum Points You Can Obtain from Cards)
Leetcode 1423.可获得的最大点数(Maximum Points You Can Obtain from Cards)
2022-04-21 06:35:00 【就叫昵称吧】
Leetcode 1423.可获得的最大点数
1 题目描述(Leetcode题目链接)
几张卡牌 排成一行,每张卡牌都有一个对应的点数。点数由整数数组 cardPoints 给出。
每次行动,你可以从行的开头或者末尾拿一张卡牌,最终你必须正好拿 k 张卡牌。
你的点数就是你拿到手中的所有卡牌的点数之和。
给你一个整数数组 cardPoints 和整数 k,请你返回可以获得的最大点数。
输入:cardPoints = [1,2,3,4,5,6,1], k = 3
输出:12
解释:第一次行动,不管拿哪张牌,你的点数总是 1 。但是,先拿最右边的卡牌将会最大化你的可获得点数。最优策略是拿右边的三张牌,最终点数为 1 + 6 + 5 = 12 。
2 题解
以k = 3为例,像下图这样遍历就行了

python
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
temp = sum(cardPoints[:k])
res = temp
n = len(cardPoints)
i = 1
while i <= k:
temp = temp - cardPoints[k - i] + cardPoints[n-i]
res = max(res, temp)
i += 1
return res
C++
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int temp = 0, n = cardPoints.size();
for(int i=0; i<k; i++){
temp += cardPoints[i];
}
int res = temp, i = 1;
while(i <= k){
temp = temp - cardPoints[k-i] + cardPoints[n-i++];
res = max(res, temp);
}
return res;
}
};
版权声明
本文为[就叫昵称吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39378221/article/details/113935655
边栏推荐
- ConvergenceWarning: Liblinear failed to converge, increase the number of iterations解决办法
- Solve the problem of Chinese garbled code
- NP, OSPF Stub Area
- 蓝桥杯——数列排序问题
- Longxun series video conversion, lt9211, lt8918, functions: LVDS to bt656, LVDS to Mipi (CSI \ DSI) RGB to Mipi (DSI \ CSI) bt656 \ 601 \ 1120 to hdmi1 4\DVI
- NP、OSPF Stub区域
- 论文阅读:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
- 【WPF】VisualStateManager
- mysql底层的引擎选择,为什么索引结构选择B+树:(3.28-4.3)
- 信息加密与信息摘要
猜你喜欢

利用安全漏洞检测工具Metasploit对靶机Metasploitable2的Telnet登录提权

Voisins bgp

PIM-DM

【WPF】利用RadioButton制作导航条

OSPF多区域

Cs5518, Mipi to dual LVDS, replacement: gm8775 of Guoteng, tc358775 of Toshiba, dual LVDS, domestic perfect replacement, DSI to dual channel LVDS, LVDS clock frequency up to 154mhz, 1920 x1200

IGMP_华为

【WPF】级联Combobox

Financial information security training - 22 / 4 / 18

【C#】文件操作
随机推荐
Domestic Poe, rpc304, replace ip802, ip804 and ip808, replace tps23861, ltc4292, pd69204 and si3459, start domestic Poe power supply and PSE, and ruipukang domestic Ethernet power supply and interface
2020-12-24
Ruiyuan ry8132 and ry9140 DCDC are mainly replaced by tps563200 and tps563201 of Ti, mp1471 and mp1653 of Xinyuan, and details of sy8104 power chip of silijie
Ruiyuan power chip, ry3715, ry3750 replacement: silijie sy7208, sy7152, Xinpeng micro ap2008 Core source mp1542, mp3213. Input voltage from 2.5V to 5.5V
Sort method ----- > Hill sort, heap sort
【WPF】利用Ellipse或Rectangle制作圆形进度条
论文阅读:Security Challenges in an Increasingly Tangled Web
PIM-DM
Ctf-rsa decryption script
信息加密与信息摘要
计算机漏洞安全相关的概念POC 、EXP 、VUL 、CVE 、0DAY
2022-1-3至2022-1-16 访问者模式
栈(C语言)
BGP neighbor
NP, OSPF route aggregation
mysql底层的引擎选择,为什么索引结构选择B+树:(3.28-4.3)
About IMAP tools module to achieve mail movement
PE header file analysis
Ms1836s, HDMI to CVBS, video converter, HDMI receiver, built-in MCU and memory
排序方式------>希尔排序,堆排序