forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1423.java
24 lines (23 loc) · 791 Bytes
/
_1423.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.fishercoder.solutions;
public class _1423 {
public static class Solution1 {
public int maxScore(int[] cardPoints, int k) {
int maxScore = 0;
if (cardPoints.length <= k) {
for (int point : cardPoints) {
maxScore += point;
}
return maxScore;
}
for (int i = 0; i < k; i++) {
maxScore += cardPoints[i];
}
int runningSum = maxScore;
for (int i = cardPoints.length - 1, j = 1; i >= 0 && j <= k; i--, j++) {
runningSum = runningSum - cardPoints[k - j] + cardPoints[i];
maxScore = Math.max(maxScore, runningSum);
}
return maxScore;
}
}
}