1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public: int maxScore(vector<int>& cardPoints, int k) { const int n = cardPoints.size(); int windowSize = n - k; int currWindowSum = accumulate(cardPoints.begin(), cardPoints.begin() + windowSize, 0); int sum = currWindowSum, minWindowSum = currWindowSum; for (int i = windowSize; i < n; ++i) { sum += cardPoints[i]; currWindowSum += cardPoints[i] - cardPoints[i - windowSize]; minWindowSum = min(minWindowSum, currWindowSum); } return sum - minWindowSum; } };
|