Skip to content

Commit a8a4ffb

Browse files
committed
add 1197 java
1 parent f251f9f commit a8a4ffb

File tree

6 files changed

+209
-12
lines changed

6 files changed

+209
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@
940940
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)
941941
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
942942
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)
943+
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)
943944
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)
944945

945946

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241102: 802
1+
20241102: 802,1197
22
20241027: 855,846
33
20241026: 932
44
20241024: 951,792

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2024-12-27 -> ['802']
1+
2024-12-27 -> ['802,1197']
22
2024-12-21 -> ['855,846']
33
2024-12-20 -> ['932']
44
2024-12-18 -> ['951,792']
@@ -7,33 +7,33 @@
77
2024-12-12 -> ['1146']
88
2024-12-08 -> ['737']
99
2024-12-07 -> ['686,734,737']
10-
2024-12-06 -> ['802', '353']
10+
2024-12-06 -> ['802,1197', '353']
1111
2024-12-05 -> ['528,334']
1212
2024-12-03 -> ['1145']
1313
2024-11-30 -> ['855,846', '1145,1219']
1414
2024-11-29 -> ['932']
1515
2024-11-27 -> ['951,792', '524,221,889']
1616
2024-11-26 -> ['743,889']
1717
2024-11-25 -> ['837']
18-
2024-11-23 -> ['802', '163,1048', '981']
18+
2024-11-23 -> ['802,1197', '163,1048', '981']
1919
2024-11-22 -> ['298,729', '1087']
2020
2024-11-21 -> ['1146']
2121
2024-11-20 -> ['939']
2222
2024-11-18 -> ['430']
2323
2024-11-17 -> ['855,846', '737', '363']
2424
2024-11-16 -> ['932', '686,734,737', '1032,844,1011']
25-
2024-11-15 -> ['802', '353', '947']
25+
2024-11-15 -> ['802,1197', '353', '947']
2626
2024-11-14 -> ['951,792', '528,334']
2727
2024-11-12 -> ['1145', '753']
2828
2024-11-11 -> ['727']
29-
2024-11-10 -> ['802', '163,1048']
29+
2024-11-10 -> ['802,1197', '163,1048']
3030
2024-11-09 -> ['855,846', '298,729', '1145,1219']
3131
2024-11-08 -> ['932', '1146']
32-
2024-11-07 -> ['802']
32+
2024-11-07 -> ['802,1197']
3333
2024-11-06 -> ['951,792', '524,221,889']
34-
2024-11-05 -> ['802', '743,889']
35-
2024-11-04 -> ['802', '855,846', '737', '837', '659']
36-
2024-11-03 -> ['802', '932', '686,734,737', '801,552']
34+
2024-11-05 -> ['802,1197', '743,889']
35+
2024-11-04 -> ['802,1197', '855,846', '737', '837', '659']
36+
2024-11-03 -> ['802,1197', '932', '686,734,737', '801,552']
3737
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
3838
2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087']
3939
2024-10-31 -> ['932', '1146']

leetcode_java/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@
3535
</plugins>
3636
</build>
3737

38-
3938
</project>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package LeetCodeJava.BFS;
2+
3+
// https://leetcode.com/problems/minimum-knight-moves/description/
4+
// https://leetcode.ca/all/1197.html
5+
6+
import java.util.ArrayDeque;
7+
import java.util.HashSet;
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
11+
/**
12+
* 1197. Minimum Knight Moves
13+
* In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0].
14+
* <p>
15+
* 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.
16+
* <p>
17+
* <p>
18+
* <p>
19+
* Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.
20+
* <p>
21+
* <p>
22+
* <p>
23+
* Example 1:
24+
* <p>
25+
* Input: x = 2, y = 1
26+
* Output: 1
27+
* Explanation: [0, 0] → [2, 1]
28+
* Example 2:
29+
* <p>
30+
* Input: x = 5, y = 5
31+
* Output: 4
32+
* Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]
33+
* <p>
34+
* <p>
35+
* Constraints:
36+
* <p>
37+
* |x| + |y| <= 300
38+
* Difficulty:
39+
* Medium
40+
* Lock:
41+
* Prime
42+
* Company:
43+
* Amazon Facebook Google Oracle
44+
* Problem Solution
45+
* 1197-Minimum-Knight-Moves
46+
*/
47+
public class MinimumKnightMoves {
48+
49+
// V0
50+
// IDEA : BFS
51+
// TODO : fix and validate
52+
// public int minKnightMoves(int x, int y) {
53+
//
54+
// if (x==0 && y==0){
55+
// return 0;
56+
// }
57+
//
58+
// // init
59+
// int[][] moves = new int[][]{ {1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2} };
60+
// // queue : FIFO
61+
// Queue<List<Integer>> q = new LinkedList<>(); // Queue([x, y, step])
62+
// List<Integer> tmp = new ArrayList<>();
63+
// tmp.add(0); // x
64+
// tmp.add(0); // y
65+
// tmp.add(0); // step
66+
// q.add(tmp);
67+
//
68+
// while(!q.isEmpty()){
69+
// List<Integer> cur = q.poll();
70+
// int cur_x = cur.get(0);
71+
// int cur_y = cur.get(1);
72+
// int cur_step = cur.get(2);
73+
// if (cur_x == x && cur_y == y){
74+
// return cur_step;
75+
// }
76+
// for (int[] move : moves){
77+
// List<Integer> newCoor = new ArrayList<>();
78+
// newCoor.add(cur_x + move[0]);
79+
// newCoor.add(cur_y + move[1]);
80+
// newCoor.add(cur_step + 1);
81+
// q.add(newCoor);
82+
// }
83+
// }
84+
//
85+
// return -1;
86+
// }
87+
88+
// V1
89+
// IDEA : BFS
90+
// https://leetcode.ca/2019-03-11-1197-Minimum-Knight-Moves/
91+
public int minKnightMoves_1(int x, int y) {
92+
x += 310;
93+
y += 310;
94+
int ans = 0;
95+
Queue<int[]> q = new ArrayDeque<>();
96+
q.offer(new int[]{310, 310});
97+
// NOTE !!! use vis (boolean 2D array) to AVOID visit same coordination again
98+
boolean[][] vis = new boolean[700][700];
99+
vis[310][310] = true;
100+
int[][] dirs = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
101+
while (!q.isEmpty()) {
102+
for (int k = q.size(); k > 0; --k) {
103+
int[] p = q.poll();
104+
if (p[0] == x && p[1] == y) {
105+
return ans;
106+
}
107+
for (int[] dir : dirs) {
108+
int c = p[0] + dir[0];
109+
int d = p[1] + dir[1];
110+
if (!vis[c][d]) {
111+
vis[c][d] = true;
112+
q.offer(new int[]{c, d});
113+
}
114+
}
115+
}
116+
++ans;
117+
}
118+
return -1;
119+
}
120+
121+
// V2
122+
// IDEA : BFD
123+
// https://www.cnblogs.com/cnoodle/p/12820573.html
124+
public int minKnightMoves_2(int x, int y) {
125+
int[][] dirs = new int[][]{{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {-2, -1}, {-2, 1}, {2, -1}, {2, 1}};
126+
x = Math.abs(x);
127+
y = Math.abs(y);
128+
HashSet<String> visited = new HashSet<>();
129+
Queue<int[]> queue = new LinkedList<>();
130+
queue.offer(new int[]{0, 0});
131+
visited.add("0,0");
132+
133+
int step = 0;
134+
while (!queue.isEmpty()) {
135+
int size = queue.size();
136+
while (size-- > 0) {
137+
int[] cur = queue.poll();
138+
if (cur[0] == x && cur[1] == y) {
139+
return step;
140+
}
141+
142+
for (int[] dir : dirs) {
143+
int i = cur[0] + dir[0];
144+
int j = cur[1] + dir[1];
145+
// (0, 0) -> (2, -1) -> (1, 1)
146+
// +2的意思是多给两个格子的空间以便于骑士跳出去再跳回来的操作
147+
if (!visited.contains(i + "," + j) && i >= -1 && j >= -1 && i <= x + 2 && j <= y + 2) {
148+
queue.offer(new int[]{i, j});
149+
visited.add(i + "," + j);
150+
}
151+
}
152+
}
153+
step++;
154+
}
155+
return -1;
156+
}
157+
158+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import LeetCodeJava.DataStructure.TreeNode;
44

5-
import java.lang.reflect.Array;
65
import java.util.*;
76

87
public class workspace5 {
@@ -2209,6 +2208,46 @@ public List<Integer> eventualSafeNodes(int[][] graph) {
22092208
return null;
22102209
}
22112210

2211+
// LC 1197
2212+
// https://leetcode.ca/all/1197.html
2213+
// 6.25 pm - 6.40 pm
2214+
// bfs
2215+
public int minKnightMoves(int x, int y) {
2216+
2217+
if (x==0 && y==0){
2218+
return 0;
2219+
}
2220+
2221+
// init
2222+
int[][] moves = new int[][]{ {1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2} };
2223+
// queue : FIFO
2224+
Queue<List<Integer>> q = new LinkedList<>(); // Queue([x, y, step])
2225+
List<Integer> tmp = new ArrayList<>();
2226+
tmp.add(0); // x
2227+
tmp.add(0); // y
2228+
tmp.add(0); // step
2229+
q.add(tmp);
2230+
2231+
while(!q.isEmpty()){
2232+
List<Integer> cur = q.poll();
2233+
int cur_x = cur.get(0);
2234+
int cur_y = cur.get(1);
2235+
int cur_step = cur.get(2);
2236+
if (cur_x == x && cur_y == y){
2237+
return cur_step;
2238+
}
2239+
for (int[] move : moves){
2240+
List<Integer> newCoor = new ArrayList<>();
2241+
newCoor.add(cur_x + move[0]);
2242+
newCoor.add(cur_y + move[1]);
2243+
newCoor.add(cur_step + 1);
2244+
q.add(newCoor);
2245+
}
2246+
}
2247+
2248+
return -1;
2249+
}
2250+
22122251

22132252
}
22142253

0 commit comments

Comments
 (0)