1423.cpp (441B)
1 class Solution { 2 public: 3 int maxScore(const vector<int> &cardPoints, int k) const { 4 const int n = size(cardPoints); 5 6 int res = 0, sum = 0; 7 for (int i = 0; i < k; i++) 8 sum += cardPoints[i]; 9 for (int i = n, j = k - 1; j >= 0; i--, j--) { 10 res = max(res, sum); 11 sum += cardPoints[i - 1]; 12 sum -= cardPoints[j]; 13 } 14 15 return max(res, sum); 16 } 17 };