From 880f1b2d2c4cb32f9ced98d447e131de57c7e9fb Mon Sep 17 00:00:00 2001 From: Trishun Date: Sat, 5 Nov 2022 21:59:49 +0100 Subject: [PATCH] Resolves Java 1D Array Pt.2 Task --- .../datastructures/array_1d_pt2/Solution.java | 63 ++++++++++++++ .../array_1d_pt2/SolutionTest.java | 83 +++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/Solution.java create mode 100644 src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/SolutionTest.java diff --git a/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/Solution.java b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/Solution.java new file mode 100644 index 0000000..7cc86ab --- /dev/null +++ b/src/main/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/Solution.java @@ -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(); + } + +} diff --git a/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/SolutionTest.java b/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/SolutionTest.java new file mode 100644 index 0000000..542511a --- /dev/null +++ b/src/test/java/eu/ztsh/training/hackerrank/datastructures/array_1d_pt2/SolutionTest.java @@ -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(); + } + + } + +} \ No newline at end of file