Skip to content

Commit ba5cfeb

Browse files
authored
Add 1690 (#140)
* Add 1690 * Fix checkstyleMain
1 parent c9ec779 commit ba5cfeb

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1690 {
4+
public static class Solution1 {
5+
6+
int[] stonesRef;
7+
8+
int[] prepareSums;
9+
10+
int[][] maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ = new int[1005][1005];
11+
12+
public int stoneGameVII(int[] stones) {
13+
this.stonesRef = stones;
14+
int totalStonesNumber = stones.length;
15+
this.prepareSums = new int[totalStonesNumber + 1];
16+
for (int i = 1; i <= totalStonesNumber; i++) {
17+
this.prepareSums[i] = this.prepareSums[i - 1] + stones[i - 1];
18+
}
19+
for (int len = 1; len <= totalStonesNumber; len++) {
20+
for (int i = 1; i + len - 1 <= totalStonesNumber; i++) {
21+
int j = i + len - 1;
22+
this.setMaxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ(i, j);
23+
}
24+
}
25+
return maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[1][totalStonesNumber];
26+
}
27+
28+
private void setMaxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ(int i, int j) {
29+
if (j - i == 0) {
30+
maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = 0;
31+
} else if (j - i == 1) {
32+
maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = Math.max(stonesRef[i - 1], stonesRef[j - 1]);
33+
} else {
34+
maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = Math.max(
35+
this.sumOfTheStonesValueInPosIToJ(i + 1, j)
36+
- maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i + 1][j],
37+
this.sumOfTheStonesValueInPosIToJ(i, j - 1)
38+
- maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j - 1]);
39+
}
40+
}
41+
42+
private int sumOfTheStonesValueInPosIToJ(int i, int j) {
43+
return this.prepareSums[j] - this.prepareSums[i - 1];
44+
}
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1690;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1690Test {
10+
private static _1690.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1690.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6, solution1.stoneGameVII(new int[]{5,3,1,4,2}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)