From a8a4ffbfaf50c95d1d27f930fb652bb7ee68bdb0 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 2 Nov 2024 18:52:52 +0800 Subject: [PATCH] add 1197 java --- README.md | 1 + data/progress.txt | 2 +- data/to_review.txt | 18 +- leetcode_java/pom.xml | 1 - .../LeetCodeJava/BFS/MinimumKnightMoves.java | 158 ++++++++++++++++++ .../src/main/java/dev/workspace5.java | 41 ++++- 6 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/BFS/MinimumKnightMoves.java diff --git a/README.md b/README.md index 1df4143d..ffef49ad 100644 --- a/README.md +++ b/README.md @@ -940,6 +940,7 @@ 994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)| [Python](./leetcode_python/Breadth-First-Search/rotting-oranges.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/RottingOranges.java) ||| Medium |BFS, dp `amazon` | OK** (2) 1110|[Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/editorial/)| [Python](./leetcode_python/Breadth-First-Search/delete_nodes_and_return_forest.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/DeleteNodesAndReturnForest.java) ||| Medium |BFS, recursive `google` | not start 1162| [As Far from Land as Possible](https://leetcode.com/problems/as-far-from-land-as-possible/) | [Python](./leetcode_python/Breadth-First-Search/as-far-from-land-as-possible.py) | | | Medium | dfs, bfs, good basic, `amazon`| AGAIN**** (3) +1197|[Minimum Knight Moves](https://leetcode.com/problems/minimum-knight-moves/description/)| [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/MinimumKnightMoves.java) ||| Medium |BFS, `google` | OK (1) 1730| [Shortest Path to Get Food](https://leetcode.com/problems/shortest-path-to-get-food/) | [Python](./leetcode_python/Breadth-First-Search/shortest-path-to-get-food.py) | | | Medium |shortest path, bfs, `amazon`| OK (2) diff --git a/data/progress.txt b/data/progress.txt index 3c530d35..1c9f681d 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241102: 802 +20241102: 802,1197 20241027: 855,846 20241026: 932 20241024: 951,792 diff --git a/data/to_review.txt b/data/to_review.txt index 60381c37..07284c20 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-12-27 -> ['802'] +2024-12-27 -> ['802,1197'] 2024-12-21 -> ['855,846'] 2024-12-20 -> ['932'] 2024-12-18 -> ['951,792'] @@ -7,7 +7,7 @@ 2024-12-12 -> ['1146'] 2024-12-08 -> ['737'] 2024-12-07 -> ['686,734,737'] -2024-12-06 -> ['802', '353'] +2024-12-06 -> ['802,1197', '353'] 2024-12-05 -> ['528,334'] 2024-12-03 -> ['1145'] 2024-11-30 -> ['855,846', '1145,1219'] @@ -15,25 +15,25 @@ 2024-11-27 -> ['951,792', '524,221,889'] 2024-11-26 -> ['743,889'] 2024-11-25 -> ['837'] -2024-11-23 -> ['802', '163,1048', '981'] +2024-11-23 -> ['802,1197', '163,1048', '981'] 2024-11-22 -> ['298,729', '1087'] 2024-11-21 -> ['1146'] 2024-11-20 -> ['939'] 2024-11-18 -> ['430'] 2024-11-17 -> ['855,846', '737', '363'] 2024-11-16 -> ['932', '686,734,737', '1032,844,1011'] -2024-11-15 -> ['802', '353', '947'] +2024-11-15 -> ['802,1197', '353', '947'] 2024-11-14 -> ['951,792', '528,334'] 2024-11-12 -> ['1145', '753'] 2024-11-11 -> ['727'] -2024-11-10 -> ['802', '163,1048'] +2024-11-10 -> ['802,1197', '163,1048'] 2024-11-09 -> ['855,846', '298,729', '1145,1219'] 2024-11-08 -> ['932', '1146'] -2024-11-07 -> ['802'] +2024-11-07 -> ['802,1197'] 2024-11-06 -> ['951,792', '524,221,889'] -2024-11-05 -> ['802', '743,889'] -2024-11-04 -> ['802', '855,846', '737', '837', '659'] -2024-11-03 -> ['802', '932', '686,734,737', '801,552'] +2024-11-05 -> ['802,1197', '743,889'] +2024-11-04 -> ['802,1197', '855,846', '737', '837', '659'] +2024-11-03 -> ['802,1197', '932', '686,734,737', '801,552'] 2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110'] 2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087'] 2024-10-31 -> ['932', '1146'] diff --git a/leetcode_java/pom.xml b/leetcode_java/pom.xml index 71ce1ff5..9085610e 100644 --- a/leetcode_java/pom.xml +++ b/leetcode_java/pom.xml @@ -35,5 +35,4 @@ - \ No newline at end of file diff --git a/leetcode_java/src/main/java/LeetCodeJava/BFS/MinimumKnightMoves.java b/leetcode_java/src/main/java/LeetCodeJava/BFS/MinimumKnightMoves.java new file mode 100644 index 00000000..ee908a63 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/BFS/MinimumKnightMoves.java @@ -0,0 +1,158 @@ +package LeetCodeJava.BFS; + +// https://leetcode.com/problems/minimum-knight-moves/description/ +// https://leetcode.ca/all/1197.html + +import java.util.ArrayDeque; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; + +/** + * 1197. Minimum Knight Moves + * In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0]. + *

+ * A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. + *

+ *

+ *

+ * Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists. + *

+ *

+ *

+ * Example 1: + *

+ * Input: x = 2, y = 1 + * Output: 1 + * Explanation: [0, 0] → [2, 1] + * Example 2: + *

+ * Input: x = 5, y = 5 + * Output: 4 + * Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5] + *

+ *

+ * Constraints: + *

+ * |x| + |y| <= 300 + * Difficulty: + * Medium + * Lock: + * Prime + * Company: + * Amazon Facebook Google Oracle + * Problem Solution + * 1197-Minimum-Knight-Moves + */ +public class MinimumKnightMoves { + + // V0 + // IDEA : BFS + // TODO : fix and validate +// public int minKnightMoves(int x, int y) { +// +// if (x==0 && y==0){ +// return 0; +// } +// +// // init +// int[][] moves = new int[][]{ {1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2} }; +// // queue : FIFO +// Queue> q = new LinkedList<>(); // Queue([x, y, step]) +// List tmp = new ArrayList<>(); +// tmp.add(0); // x +// tmp.add(0); // y +// tmp.add(0); // step +// q.add(tmp); +// +// while(!q.isEmpty()){ +// List cur = q.poll(); +// int cur_x = cur.get(0); +// int cur_y = cur.get(1); +// int cur_step = cur.get(2); +// if (cur_x == x && cur_y == y){ +// return cur_step; +// } +// for (int[] move : moves){ +// List newCoor = new ArrayList<>(); +// newCoor.add(cur_x + move[0]); +// newCoor.add(cur_y + move[1]); +// newCoor.add(cur_step + 1); +// q.add(newCoor); +// } +// } +// +// return -1; +// } + + // V1 + // IDEA : BFS + // https://leetcode.ca/2019-03-11-1197-Minimum-Knight-Moves/ + public int minKnightMoves_1(int x, int y) { + x += 310; + y += 310; + int ans = 0; + Queue q = new ArrayDeque<>(); + q.offer(new int[]{310, 310}); + // NOTE !!! use vis (boolean 2D array) to AVOID visit same coordination again + boolean[][] vis = new boolean[700][700]; + vis[310][310] = true; + int[][] dirs = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; + while (!q.isEmpty()) { + for (int k = q.size(); k > 0; --k) { + int[] p = q.poll(); + if (p[0] == x && p[1] == y) { + return ans; + } + for (int[] dir : dirs) { + int c = p[0] + dir[0]; + int d = p[1] + dir[1]; + if (!vis[c][d]) { + vis[c][d] = true; + q.offer(new int[]{c, d}); + } + } + } + ++ans; + } + return -1; + } + + // V2 + // IDEA : BFD + // https://www.cnblogs.com/cnoodle/p/12820573.html + public int minKnightMoves_2(int x, int y) { + int[][] dirs = new int[][]{{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {-2, -1}, {-2, 1}, {2, -1}, {2, 1}}; + x = Math.abs(x); + y = Math.abs(y); + HashSet visited = new HashSet<>(); + Queue queue = new LinkedList<>(); + queue.offer(new int[]{0, 0}); + visited.add("0,0"); + + int step = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + while (size-- > 0) { + int[] cur = queue.poll(); + if (cur[0] == x && cur[1] == y) { + return step; + } + + for (int[] dir : dirs) { + int i = cur[0] + dir[0]; + int j = cur[1] + dir[1]; + // (0, 0) -> (2, -1) -> (1, 1) + // +2的意思是多给两个格子的空间以便于骑士跳出去再跳回来的操作 + if (!visited.contains(i + "," + j) && i >= -1 && j >= -1 && i <= x + 2 && j <= y + 2) { + queue.offer(new int[]{i, j}); + visited.add(i + "," + j); + } + } + } + step++; + } + return -1; + } + +} diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 32b95eb8..041e69e4 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -2,7 +2,6 @@ import LeetCodeJava.DataStructure.TreeNode; -import java.lang.reflect.Array; import java.util.*; public class workspace5 { @@ -2209,6 +2208,46 @@ public List eventualSafeNodes(int[][] graph) { return null; } + // LC 1197 + // https://leetcode.ca/all/1197.html + // 6.25 pm - 6.40 pm + // bfs + public int minKnightMoves(int x, int y) { + + if (x==0 && y==0){ + return 0; + } + + // init + int[][] moves = new int[][]{ {1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2} }; + // queue : FIFO + Queue> q = new LinkedList<>(); // Queue([x, y, step]) + List tmp = new ArrayList<>(); + tmp.add(0); // x + tmp.add(0); // y + tmp.add(0); // step + q.add(tmp); + + while(!q.isEmpty()){ + List cur = q.poll(); + int cur_x = cur.get(0); + int cur_y = cur.get(1); + int cur_step = cur.get(2); + if (cur_x == x && cur_y == y){ + return cur_step; + } + for (int[] move : moves){ + List newCoor = new ArrayList<>(); + newCoor.add(cur_x + move[0]); + newCoor.add(cur_y + move[1]); + newCoor.add(cur_step + 1); + q.add(newCoor); + } + } + + return -1; + } + }