-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxScore.cpp
More file actions
30 lines (29 loc) · 960 Bytes
/
maxScore.cpp
File metadata and controls
30 lines (29 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int windowSum = 0, s = cardPoints.size() - k;
for (int i = 0; i < s; ++i)
windowSum += cardPoints[i];
int totalSum = windowSum, minSum = windowSum;
for (int l = 0, i = s; i < cardPoints.size(); ++i, ++l) {
totalSum += cardPoints[i];
windowSum += cardPoints[i] - cardPoints[l];
minSum = std::min(minSum, windowSum);
}
return totalSum - minSum;
}
};
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int windowSum = 0, n = cardPoints.size();
for (int i = 0; i < k; ++i)
windowSum += cardPoints[i];
int maxSum = windowSum;
for (int i = 0; i < k; ++i) {
windowSum += cardPoints[n - i - 1] - cardPoints[k - i - 1];
maxSum = std::max(maxSum, windowSum);
}
return maxSum;
}
};