Skip to content

Commit

Permalink
add 1197 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 2, 2024
1 parent f251f9f commit a8a4ffb
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20241102: 802
20241102: 802,1197
20241027: 855,846
20241026: 932
20241024: 951,792
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -7,33 +7,33 @@
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']
2024-11-29 -> ['932']
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']
Expand Down
1 change: 0 additions & 1 deletion leetcode_java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@
</plugins>
</build>


</project>
158 changes: 158 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/BFS/MinimumKnightMoves.java
Original file line number Diff line number Diff line change
@@ -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].
* <p>
* 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.
* <p>
* <p>
* <p>
* Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* Input: x = 2, y = 1
* Output: 1
* Explanation: [0, 0] → [2, 1]
* Example 2:
* <p>
* Input: x = 5, y = 5
* Output: 4
* Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]
* <p>
* <p>
* Constraints:
* <p>
* |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<List<Integer>> q = new LinkedList<>(); // Queue([x, y, step])
// List<Integer> tmp = new ArrayList<>();
// tmp.add(0); // x
// tmp.add(0); // y
// tmp.add(0); // step
// q.add(tmp);
//
// while(!q.isEmpty()){
// List<Integer> 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<Integer> 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<int[]> 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<String> visited = new HashSet<>();
Queue<int[]> 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;
}

}
41 changes: 40 additions & 1 deletion leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import LeetCodeJava.DataStructure.TreeNode;

import java.lang.reflect.Array;
import java.util.*;

public class workspace5 {
Expand Down Expand Up @@ -2209,6 +2208,46 @@ public List<Integer> 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<List<Integer>> q = new LinkedList<>(); // Queue([x, y, step])
List<Integer> tmp = new ArrayList<>();
tmp.add(0); // x
tmp.add(0); // y
tmp.add(0); // step
q.add(tmp);

while(!q.isEmpty()){
List<Integer> 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<Integer> 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;
}


}

Expand Down

0 comments on commit a8a4ffb

Please sign in to comment.