当前位置:网站首页>Leetcode 1423. Maximum points you can obtain from cards
Leetcode 1423. Maximum points you can obtain from cards
2022-04-22 05:14:00 【Just call it a nickname】
Leetcode 1423. The maximum number of points available
1 Title Description (Leetcode Topic link )
How many cards Line up , Each card has a corresponding number of points . The number of points is an array of integers cardPoints give .
Every action , You can take a card from the beginning or the end of the line , In the end, you have to just take k Cards .
Your number of points is the sum of the number of points you get from all the cards in your hand .
Give you an array of integers cardPoints And integer k, Please return the maximum number of points you can get .
Input :cardPoints = [1,2,3,4,5,6,1], k = 3
Output :12
explain : For the first time , No matter which card you take , Your points are always 1 . however , Taking the card on the far right first will maximize your points . The best strategy is to take the three cards on the right , The final point is 1 + 6 + 5 = 12 .
2 Answer key
With k = 3 For example , Just traverse like the following figure

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;
}
};
版权声明
本文为[Just call it a nickname]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626349816.html
边栏推荐
- Onzième emploi dans la base de données MySQL - application de la vue
- [Qualcomm sdm660 platform] (8) -- Introduction to camera metadata
- Basic concepts of outh2
- Western dichotomy and Eastern trisection
- The eleventh job of MySQL database - Application of view
- All conditions that trigger epollin and epollout
- Prometheus basic knowledge brain map
- Vs2019 official free print control
- Servlet lifecycle
- JUnit assertion
猜你喜欢

QBoxSet、QBoxPlotSeries

Esp32 vhci realizes ble broadcasting, which is so magical

A collection of common methods of data exploratory analysis (EDA)

Supplement to the usage of permute() function in torch (detailed process of matrix dimension change)

Introduction to swagger UI

Spark 入門程序 : WordCount
What are the challenges of Internet of things testing, and how do software testing institutions ensure quality

Junit Introduction et Introduction

JS Chapter 12

Summary of browser cross domain problems
随机推荐
Acrobat Pro DC tutorial: how to create PDF using text and picture files?
Query result processing
VIM is so difficult to use, why are so many people keen?
Autojs cannot connect to the computer, prompting that the connection failed and the permission is insufficient
How to initiate mqtt 100 million connection and 10 million message throughput performance test
2021-10-17
Summary of common methods, advantages and disadvantages of robot UAV visual obstacle avoidance
Chapter V function
MySQL數據庫第十一次作業-視圖的應用
Considerations for importing idea package and calling its methods
[matlab] draw Zernike polynomials
Chapter VIII affairs
Go语言爬虫基础
Request and response objects
Junit Introduction et Introduction
Clonal map of writing in mind
Chapter I overview of database
数据库第十三次作业 事务管理
Mongodb experiment -- data backup and recovery and database optimization
Esp32 vhci realizes ble broadcasting, which is so magical