Skip to content

Add 1690 #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1690.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.fishercoder.solutions;

public class _1690 {
public static class Solution1 {

int[] stonesRef;

int[] prepareSums;

int[][] maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ = new int[1005][1005];

public int stoneGameVII(int[] stones) {
this.stonesRef = stones;
int totalStonesNumber = stones.length;
this.prepareSums = new int[totalStonesNumber + 1];
for (int i = 1; i <= totalStonesNumber; i++) {
this.prepareSums[i] = this.prepareSums[i - 1] + stones[i - 1];
}
for (int len = 1; len <= totalStonesNumber; len++) {
for (int i = 1; i + len - 1 <= totalStonesNumber; i++) {
int j = i + len - 1;
this.setMaxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ(i, j);
}
}
return maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[1][totalStonesNumber];
}

private void setMaxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ(int i, int j) {
if (j - i == 0) {
maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = 0;
} else if (j - i == 1) {
maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = Math.max(stonesRef[i - 1], stonesRef[j - 1]);
} else {
maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j] = Math.max(
this.sumOfTheStonesValueInPosIToJ(i + 1, j)
- maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i + 1][j],
this.sumOfTheStonesValueInPosIToJ(i, j - 1)
- maxDiffScoureBetweenTowPlayerWhenPlayInPosItoJ[i][j - 1]);
}
}

private int sumOfTheStonesValueInPosIToJ(int i, int j) {
return this.prepareSums[j] - this.prepareSums[i - 1];
}
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/fishercoder/_1690Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.fishercoder;

import com.fishercoder.solutions._1690;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class _1690Test {
private static _1690.Solution1 solution1;

@BeforeClass
public static void setup() {
solution1 = new _1690.Solution1();
}

@Test
public void test1() {
assertEquals(6, solution1.stoneGameVII(new int[]{5,3,1,4,2}));
}

}