1423. Maximum Points You Can Obtain from Cards
Solution
Consider the left part and right part combination and find the max with the sliding window technique.
public int maxScore(int[] cardPoints, int k) {
int max = 0, score = 0, n = cardPoints.length;
for(int i = 0; i < k; i++){
score += cardPoints[i];
}
max = score;
if( n == k ) return max;
for(int i = 1; i <= k; i++){
score += cardPoints[n-i] - cardPoints[k-i];
max = Math.max(score, max);
}
return max;
}
Last updated
Was this helpful?