From fc7f6bfd02d3ffdad61d276dc0d21ba5b8671418 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 25 Feb 2024 04:47:19 +0200 Subject: [PATCH 1/5] Added task 3001 --- .../Solution.java | 29 +++++++++++ .../readme.md | 51 +++++++++++++++++++ .../SolutionTest.java | 18 +++++++ 3 files changed, 98 insertions(+) create mode 100644 src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java create mode 100644 src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md create mode 100644 src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java diff --git a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java new file mode 100644 index 000000000..1794aed30 --- /dev/null +++ b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java @@ -0,0 +1,29 @@ +package g3001_3100.s3001_minimum_moves_to_capture_the_queen; + +// #Medium #Array #Enumeration #2024_02_25_Time_0_ms_(100.00%)_Space_40.7_MB_(78.00%) + +public class Solution { + public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { + if (a == e || b == f) { + if (a == c) { + if ((d > b && d < f) || (d > f && d < b)) { + return 2; + } + } + if (b == d) { + if ((c > a && c < e) || (c > e && c < a)) { + return 2; + } + } + return 1; + } else if (Math.abs(c - e) == Math.abs(d - f)) { + if (Math.abs(a - c) == Math.abs(b - d) && Math.abs(e - a) == Math.abs(f - b)) { + if ((a > e && a < c) || (a > c && a < e)) { + return 2; + } + } + return 1; + } + return 2; + } +} diff --git a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md new file mode 100644 index 000000000..4978cdba0 --- /dev/null +++ b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/readme.md @@ -0,0 +1,51 @@ +3001\. Minimum Moves to Capture The Queen + +Medium + +There is a **1-indexed** `8 x 8` chessboard containing `3` pieces. + +You are given `6` integers `a`, `b`, `c`, `d`, `e`, and `f` where: + +* `(a, b)` denotes the position of the white rook. +* `(c, d)` denotes the position of the white bishop. +* `(e, f)` denotes the position of the black queen. + +Given that you can only move the white pieces, return _the **minimum** number of moves required to capture the black queen_. + +**Note** that: + +* Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces. +* Bishops can move any number of squares diagonally, but cannot jump over other pieces. +* A rook or a bishop can capture the queen if it is located in a square that they can move to. +* The queen does not move. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/12/21/ex1.png) + +**Input:** a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 + +**Output:** 2 + +**Explanation:** We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3). + +It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2023/12/21/ex2.png) + +**Input:** a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 + +**Output:** 1 + +**Explanation:** We can capture the black queen in a single move by doing one of the following: + +- Move the white rook to (5, 2). + +- Move the white bishop to (5, 2). + +**Constraints:** + +* `1 <= a, b, c, d, e, f <= 8` +* No two pieces are on the same square. \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java new file mode 100644 index 000000000..ff5a3fbde --- /dev/null +++ b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3001_minimum_moves_to_capture_the_queen; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMovesToCaptureTheQueen() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 8, 8, 2, 3), equalTo(2)); + } + + @Test + void minMovesToCaptureTheQueen2() { + assertThat(new Solution().minMovesToCaptureTheQueen(5, 3, 3, 4, 5, 2), equalTo(1)); + } +} From 97525823d9965713550e0f4f099241b7b79e3c29 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 25 Feb 2024 05:04:22 +0200 Subject: [PATCH 2/5] Improved tests --- .../SolutionTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java index ff5a3fbde..cd9769ca3 100644 --- a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java +++ b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java @@ -15,4 +15,39 @@ void minMovesToCaptureTheQueen() { void minMovesToCaptureTheQueen2() { assertThat(new Solution().minMovesToCaptureTheQueen(5, 3, 3, 4, 5, 2), equalTo(1)); } + + @Test + void minMovesToCaptureTheQueen3() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 1), equalTo(2)); + } + + @Test + void minMovesToCaptureTheQueen4() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 1, 5), equalTo(2)); + } + + @Test + void minMovesToCaptureTheQueen5() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 5), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen6() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 3), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen7() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 3, 5), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen8() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 1), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen9() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 2, 3, 5, 5), equalTo(2)); + } } From f36253c78093602cfa491f429012a6da37bd1bb6 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 25 Feb 2024 05:15:37 +0200 Subject: [PATCH 3/5] Improved tests --- .../Solution.java | 19 +++++++------------ .../SolutionTest.java | 17 ++++++----------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java index 1794aed30..6450b192f 100644 --- a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java +++ b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java @@ -5,22 +5,17 @@ public class Solution { public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { if (a == e || b == f) { - if (a == c) { - if ((d > b && d < f) || (d > f && d < b)) { - return 2; - } + if (a == c && ((d > b && d < f) || (d > f && d < b))) { + return 2; } - if (b == d) { - if ((c > a && c < e) || (c > e && c < a)) { - return 2; - } + if (b == d && ((c > a && c < e) || (c > e && c < a))) { + return 2; } return 1; } else if (Math.abs(c - e) == Math.abs(d - f)) { - if (Math.abs(a - c) == Math.abs(b - d) && Math.abs(e - a) == Math.abs(f - b)) { - if ((a > e && a < c) || (a > c && a < e)) { - return 2; - } + if (Math.abs(a - c) == Math.abs(b - d) && Math.abs(e - a) == Math.abs(f - b) + && ((a > e && a < c) || (a > c && a < e))) { + return 2; } return 1; } diff --git a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java index cd9769ca3..349c876ed 100644 --- a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java +++ b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java @@ -18,36 +18,31 @@ void minMovesToCaptureTheQueen2() { @Test void minMovesToCaptureTheQueen3() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 1), equalTo(2)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 4, 1, 5), equalTo(1)); } @Test void minMovesToCaptureTheQueen4() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 1, 5), equalTo(2)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 2, 4, 2), equalTo(2)); } @Test void minMovesToCaptureTheQueen5() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 5), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 4, 5, 3, 6), equalTo(1)); } @Test void minMovesToCaptureTheQueen6() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 3), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 4, 5, 6), equalTo(1)); } @Test void minMovesToCaptureTheQueen7() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 3, 5), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 2, 4, 3, 3), equalTo(1)); } @Test void minMovesToCaptureTheQueen8() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 1), equalTo(1)); - } - - @Test - void minMovesToCaptureTheQueen9() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 2, 3, 5, 5), equalTo(2)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 3, 2, 4), equalTo(1)); } } From ab598ce0a2a76fa427f09911cd3fd4188966a502 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 25 Feb 2024 05:16:20 +0200 Subject: [PATCH 4/5] Fixed style --- .../s3001_minimum_moves_to_capture_the_queen/Solution.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java index 6450b192f..b978d34b1 100644 --- a/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java +++ b/src/main/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/Solution.java @@ -13,8 +13,9 @@ public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { } return 1; } else if (Math.abs(c - e) == Math.abs(d - f)) { - if (Math.abs(a - c) == Math.abs(b - d) && Math.abs(e - a) == Math.abs(f - b) - && ((a > e && a < c) || (a > c && a < e))) { + if (Math.abs(a - c) == Math.abs(b - d) + && Math.abs(e - a) == Math.abs(f - b) + && ((a > e && a < c) || (a > c && a < e))) { return 2; } return 1; From fee62433a06dcc2422b63057b446ce36aa01d7da Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 25 Feb 2024 05:20:10 +0200 Subject: [PATCH 5/5] Improved tests --- .../SolutionTest.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java index 349c876ed..cd9769ca3 100644 --- a/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java +++ b/src/test/java/g3001_3100/s3001_minimum_moves_to_capture_the_queen/SolutionTest.java @@ -18,31 +18,36 @@ void minMovesToCaptureTheQueen2() { @Test void minMovesToCaptureTheQueen3() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 4, 1, 5), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 1), equalTo(2)); } @Test void minMovesToCaptureTheQueen4() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 2, 4, 2), equalTo(2)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 1, 5), equalTo(2)); } @Test void minMovesToCaptureTheQueen5() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 4, 5, 3, 6), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 5), equalTo(1)); } @Test void minMovesToCaptureTheQueen6() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 4, 5, 6), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 1, 5, 3), equalTo(1)); } @Test void minMovesToCaptureTheQueen7() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 2, 4, 3, 3), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 1, 3, 3, 5), equalTo(1)); } @Test void minMovesToCaptureTheQueen8() { - assertThat(new Solution().minMovesToCaptureTheQueen(1, 2, 3, 3, 2, 4), equalTo(1)); + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 3, 3, 5, 1), equalTo(1)); + } + + @Test + void minMovesToCaptureTheQueen9() { + assertThat(new Solution().minMovesToCaptureTheQueen(1, 1, 2, 3, 5, 5), equalTo(2)); } }