Skip to content

Commit fd70090

Browse files
authored
Create Maximum Points You Can Obtain from Cards.java
1 parent cbf4633 commit fd70090

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxScore(int[] cardPoints, int k) {
3+
int n = cardPoints.length;
4+
int[] prefixSum = new int[k + 1];
5+
int[] suffixSum = new int[k + 1];
6+
for (int i = 0; i < k; i++) {
7+
prefixSum[i + 1] = cardPoints[i] + prefixSum[i];
8+
suffixSum[i + 1] = cardPoints[n - i - 1] + suffixSum[i];
9+
}
10+
int result = 0;
11+
for (int i = 0; i <= k; i++) {
12+
int currScore = prefixSum[i] + suffixSum[k - i];
13+
result = Math.max(result, currScore);
14+
}
15+
return result;
16+
}
17+
}

0 commit comments

Comments
 (0)