Skip to content

Commit 05e064b

Browse files
refactor 317
1 parent f9d5858 commit 05e064b

File tree

1 file changed

+46
-40
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+46
-40
lines changed

src/main/java/com/fishercoder/solutions/_317.java

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,64 @@ The point (1,2) is an ideal empty land to build a house, as the total travel dis
2828
*/
2929

3030
public class _317 {
31+
public static class Solution1 {
32+
public int shortestDistance(int[][] grid) {
33+
int m = grid.length;
34+
if (m == 0) {
35+
return -1;
36+
}
37+
int n = grid[0].length;
38+
int[][] reach = new int[m][n];
39+
int[][] distance = new int[m][n];
40+
int[] shift = new int[] {0, 1, 0, -1,
41+
0};//how these five elements is ordered is important since it denotes the neighbor of the current node
42+
int numBuilding = 0;
3143

32-
public int shortestDistance(int[][] grid) {
33-
int m = grid.length;
34-
if (m == 0) {
35-
return -1;
36-
}
37-
int n = grid[0].length;
38-
int[][] reach = new int[m][n];
39-
int[][] distance = new int[m][n];
40-
int[] shift = new int[]{0, 1, 0, -1, 0};//how these five elements is ordered is important since it denotes the neighbor of the current node
41-
int numBuilding = 0;
42-
43-
for (int i = 0; i < m; i++) {
44-
for (int j = 0; j < n; j++) {
45-
if (grid[i][j] == 1) {
46-
numBuilding++;
47-
int dist = 1;
48-
boolean[][] visited = new boolean[m][n];
44+
for (int i = 0; i < m; i++) {
45+
for (int j = 0; j < n; j++) {
46+
if (grid[i][j] == 1) {
47+
numBuilding++;
48+
int dist = 1;
49+
boolean[][] visited = new boolean[m][n];
4950

50-
Queue<int[]> q = new LinkedList<int[]>();
51-
q.offer(new int[]{i, j});
52-
while (!q.isEmpty()) {
53-
int size = q.size();
54-
for (int l = 0; l < size; l++) {
55-
int[] current = q.poll();
56-
for (int k = 0; k < 4; k++) {
57-
int nextRow = current[0] + shift[k];
58-
int nextCol = current[1] + shift[k + 1];
59-
if (nextRow >= 0 && nextRow < m && nextCol >= 0
60-
&& nextCol < n && !visited[nextRow][nextCol] && grid[nextRow][nextCol] == 0) {
61-
distance[nextRow][nextCol] += dist;
62-
visited[nextRow][nextCol] = true;
63-
reach[nextRow][nextCol]++;
64-
q.offer(new int[]{nextRow, nextCol});
51+
Queue<int[]> q = new LinkedList<int[]>();
52+
q.offer(new int[] {i, j});
53+
while (!q.isEmpty()) {
54+
int size = q.size();
55+
for (int l = 0; l < size; l++) {
56+
int[] current = q.poll();
57+
for (int k = 0; k < 4; k++) {
58+
int nextRow = current[0] + shift[k];
59+
int nextCol = current[1] + shift[k + 1];
60+
if (nextRow >= 0
61+
&& nextRow < m
62+
&& nextCol >= 0
63+
&& nextCol < n
64+
&& !visited[nextRow][nextCol]
65+
&& grid[nextRow][nextCol] == 0) {
66+
distance[nextRow][nextCol] += dist;
67+
visited[nextRow][nextCol] = true;
68+
reach[nextRow][nextCol]++;
69+
q.offer(new int[] {nextRow, nextCol});
70+
}
6571
}
6672
}
73+
dist++;
6774
}
68-
dist++;
6975
}
7076
}
7177
}
72-
}
7378

74-
int result = Integer.MAX_VALUE;
75-
for (int i = 0; i < m; i++) {
76-
for (int j = 0; j < n; j++) {
77-
if (grid[i][j] == 0 && reach[i][j] == numBuilding && distance[i][j] < result) {
78-
result = distance[i][j];
79+
int result = Integer.MAX_VALUE;
80+
for (int i = 0; i < m; i++) {
81+
for (int j = 0; j < n; j++) {
82+
if (grid[i][j] == 0 && reach[i][j] == numBuilding && distance[i][j] < result) {
83+
result = distance[i][j];
84+
}
7985
}
8086
}
87+
return result == Integer.MAX_VALUE ? -1 : result;
8188
}
82-
return result == Integer.MAX_VALUE ? -1 : result;
8389
}
8490

8591
}

0 commit comments

Comments
 (0)