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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class Solution { public int maxScore(int[] cardPoints, int k) { int n = cardPoints.length; int result = 0; if (k >= n) { for (int i = 0; i < n; i++) { result += cardPoints[i]; } return result; }
int l = 0; int r = n - k - 1;
int minValue = Integer.MAX_VALUE; int x = 0; while (r < n) { int ans = 0; if (x == 0) { for (int i = l; i <= r; i++) { ans += cardPoints[i]; } x = ans; }else { ans = x - cardPoints[l-1] + cardPoints[r]; x = ans; }
minValue = Math.min(minValue, ans);
l++; r++; }
int max = 0; for (int i = 0; i < n; i++) { max += cardPoints[i]; }
return max - minValue; } }
|