Skip to content

Commit 2922a4b

Browse files
add 1423
1 parent 2a0b50d commit 2922a4b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1423.java) | |Medium|Array, DP, Sliding Window|
1112
|1422|[Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1422.java) | |Easy|String|
1213
|1418|[Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1418.java) | |Medium|HashTable|
1314
|1417|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1417.java) | |Easy|String|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1423 {
4+
public static class Solution1 {
5+
public int maxScore(int[] cardPoints, int k) {
6+
int maxScore = 0;
7+
if (cardPoints.length <= k) {
8+
for (int point : cardPoints) {
9+
maxScore += point;
10+
}
11+
return maxScore;
12+
}
13+
for (int i = 0; i < k; i++) {
14+
maxScore += cardPoints[i];
15+
}
16+
int runningSum = maxScore;
17+
for (int i = cardPoints.length - 1, j = 1; i >= 0 && j <= k; i--, j++) {
18+
runningSum = runningSum - cardPoints[k - j] + cardPoints[i];
19+
maxScore = Math.max(maxScore, runningSum);
20+
}
21+
return maxScore;
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1423;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1423Test {
10+
private static _1423.Solution1 solution1;
11+
private static int[] cardPoints;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1423.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
cardPoints = new int[]{1, 2, 3, 4, 5, 6, 1};
21+
assertEquals(12, solution1.maxScore(cardPoints, 3));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)