Resolves Java 1D Array Pt.2 Task

This commit is contained in:
Piotr Dec 2022-11-05 21:59:49 +01:00
parent 1279712a8f
commit 880f1b2d2c
2 changed files with 146 additions and 0 deletions

View file

@ -0,0 +1,63 @@
package eu.ztsh.training.hackerrank.datastructures.array_1d_pt2;
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static boolean canWin(int leap, int[] game) {
return move(0, leap, game, new int[]{});
}
static boolean isWinningMove(int index, int leap, int length) {
return index == length - 1 || index + leap > length - 1;
}
static boolean canMoveForward(int index, int[] game) {
return game[index + 1] == 0; // We don't check last index as it is considered as winning move
}
static boolean canMoveBackward(int index, int[] game) {
return index > 0 && game[index - 1] == 0;
}
static boolean canJump(int index, int leap, int[] game) {
return game[index + leap] == 0; // We don't bother here about index overflow too
}
private static boolean move(int index, int leap, int[] game, int[] history) {
if (Arrays.stream(history).anyMatch(i -> i == index)) {
return false;
}
var newHistory = Arrays.copyOf(history, history.length + 1);
newHistory[newHistory.length - 1] = index;
if (isWinningMove(index, leap, game.length)) {
return true;
}
if (canMoveForward(index, game) && move(index + 1, leap, game, newHistory)) {
return true;
}
if (canJump(index, leap, game) && move(index + leap, leap, game, newHistory)) {
return true;
}
return canMoveBackward(index, game) && move(index - 1, leap, game, newHistory);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
while (q-- > 0) {
int n = scan.nextInt();
int leap = scan.nextInt();
int[] game = new int[n];
for (int i = 0; i < n; i++) {
game[i] = scan.nextInt();
}
System.out.println((canWin(leap, game)) ? "YES" : "NO");
}
scan.close();
}
}

View file

@ -0,0 +1,83 @@
package eu.ztsh.training.hackerrank.datastructures.array_1d_pt2;
import java.util.List;
import eu.ztsh.training.hackerrank.HackerRankTest;
import eu.ztsh.training.hackerrank.SolutionClassDescription;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class SolutionTest extends HackerRankTest {
@Override
protected SolutionClassDescription getSolutionClassDescription() {
return new SolutionClassDescription(Solution.class);
}
@Nested
@DisplayName("Provided test cases")
class TestCases {
@Test
public void testCase0() {
simpleAssert(
List.of(
"4",
"5 3",
"0 0 0 0 0",
"6 5",
"0 0 0 1 1 1",
"6 3",
"0 0 1 1 1 0",
"3 1",
"0 1 0"
),
List.of(
"YES",
"YES",
"NO",
"NO"
)
);
}
}
@Nested
@DisplayName("Unit tests")
class UnitTests {
@Test
public void winningMoveTest() {
assertThat(Solution.isWinningMove(0, 1, 2)).isFalse();
assertThat(Solution.isWinningMove(1, 5, 6)).isTrue();
}
@Test
public void moveForwardTest() {
var game = new int[]{0, 0, 1};
assertThat(Solution.canMoveForward(0, game)).isTrue();
assertThat(Solution.canMoveForward(1, game)).isFalse();
}
@Test
public void moveBackwardTest() {
var game = new int[]{0, 1, 0, 0, 1};
assertThat(Solution.canMoveBackward(0, game)).isFalse();
assertThat(Solution.canMoveBackward(3, game)).isTrue();
assertThat(Solution.canMoveBackward(2, game)).isFalse();
}
@Test
public void jumpTest() {
var game = new int[]{0, 0, 1, 1, 1, 0};
assertThat(Solution.canJump(0, 3, game)).isFalse();
assertThat(Solution.canJump(1, 3, game)).isFalse();
assertThat(Solution.canJump(1, 4, game)).isTrue();
}
}
}