Skip to content

Commit 47a063d

Browse files
author
Tushar Roy
committed
Update shortest distance from all buildings code
1 parent f7aebb6 commit 47a063d

File tree

1 file changed

+46
-66
lines changed

1 file changed

+46
-66
lines changed

src/com/interview/multiarray/ShortestDistanceFromAllBuildings.java

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.util.ArrayList;
44
import java.util.Deque;
5+
import java.util.HashSet;
56
import java.util.LinkedList;
67
import java.util.List;
8+
import java.util.Queue;
9+
import java.util.Set;
710

811
/**
912
* Date 03/25/2016
@@ -13,90 +16,67 @@
1316
* https://leetcode.com/problems/shortest-distance-from-all-buildings/
1417
*/
1518
public class ShortestDistanceFromAllBuildings {
16-
public int shortestDistance(int[][] grid) {
17-
int countBuilding = 0;
18-
List<Point> buildings = new ArrayList<>();
19-
for (int i = 0; i < grid.length ; i++) {
20-
for (int j = 0; j < grid[0].length; j++) {
21-
if (grid[i][j] == 1) {
22-
countBuilding++;
23-
buildings.add(new Point(i, j, 0));
24-
}
25-
}
26-
}
27-
28-
if (countBuilding == 0) {
29-
return -1;
19+
private class Point {
20+
int x, y, dist = 0;
21+
Point(int row, int col, int dist) {
22+
x = row;
23+
y = col;
24+
this.dist = dist;
3025
}
26+
}
3127

32-
int[][] output = new int[grid.length][grid[0].length];
28+
public int shortestDistance(int[][] grid) {
29+
int totalBuild = 0;
3330
int[][] reach = new int[grid.length][grid[0].length];
34-
for (Point building : buildings) {
35-
Deque<Point> queue = new LinkedList<>();
36-
queue.offerFirst(building);
37-
int currentBuildingCount = 0;
38-
boolean[][] visited = new boolean[output.length][output[0].length];
39-
visited[building.x][building.y] = true;
40-
while (!queue.isEmpty()) {
41-
Point p = queue.pollLast();
42-
output[p.x][p.y] += p.d;
43-
List<Point> neighbors = neighbors(p, grid.length, grid[0].length);
44-
for (Point neighbor : neighbors) {
45-
if (visited[neighbor.x][neighbor.y]) {
46-
continue;
31+
int[][] distance = new int[grid.length][grid[0].length];
32+
for(int i = 0; i < grid.length; i++) {
33+
for(int j = 0; j < grid[0].length; j++) {
34+
if(grid[i][j] == 1) {
35+
Queue<Point> queue = new LinkedList<>();
36+
queue.offer(new Point(i, j, 0));
37+
totalBuild++;
38+
boolean[][] visited = new boolean[grid.length][grid[0].length];
39+
while (!queue.isEmpty()) {
40+
List<Point> neighbors = getNeighbors(queue.poll(), grid, visited, grid.length, grid[0].length);
41+
for (Point neigh : neighbors) {
42+
visited[neigh.x][neigh.y] = true;
43+
reach[neigh.x][neigh.y]++;
44+
distance[neigh.x][neigh.y] += neigh.dist;
45+
queue.offer(neigh);
46+
}
4747
}
48-
if (grid[neighbor.x][neighbor.y] == 1) {
49-
currentBuildingCount++;
50-
} else if (grid[neighbor.x][neighbor.y] != 2) {
51-
queue.offerFirst(neighbor);
52-
reach[neighbor.x][neighbor.y] += 1;
53-
}
54-
visited[neighbor.x][neighbor.y] = true;
5548
}
5649
}
57-
if (countBuilding - 1 != currentBuildingCount) {
58-
return -1;
59-
}
6050
}
51+
6152
int min = Integer.MAX_VALUE;
62-
for (int i = 0; i < output.length; i++) {
63-
for (int j = 0; j < output[0].length; j++) {
64-
if (reach[i][j] == countBuilding) {
65-
min = Math.min(min, output[i][j]);
53+
for (int i = 0; i < reach.length; i++) {
54+
for (int j = 0; j < reach[0].length; j++) {
55+
if (reach[i][j] == totalBuild) {
56+
min = Math.min(min, distance[i][j]);
6657
}
6758
}
6859
}
60+
6961
return min == Integer.MAX_VALUE ? -1 : min;
7062
}
7163

72-
private List<Point> neighbors(Point p, int m, int n) {
73-
List<Point> neighbors = new ArrayList<>();
74-
if (p.y < n - 1) {
75-
neighbors.add(new Point(p.x, p.y + 1, p.d + 1));
76-
}
77-
if (p.x < m - 1) {
78-
neighbors.add(new Point(p.x + 1, p.y, p.d + 1));
79-
}
80-
if (p.x > 0) {
81-
neighbors.add(new Point(p.x - 1, p.y, p.d + 1));
82-
}
83-
if (p.y > 0) {
84-
neighbors.add(new Point(p.x, p.y - 1, p.d + 1));
85-
}
86-
return neighbors;
87-
}
64+
private List<Point> getNeighbors(Point p, int[][] grid, boolean[][] visited, int n, int m) {
65+
List<Point> resultList = new ArrayList<>();
66+
if(p.x > 0 && grid[p.x -1][p.y] == 0 && !visited[p.x - 1][p.y])
67+
resultList.add(new Point(p.x -1, p.y, p.dist + 1));
68+
if(p.x < n - 1 && grid[p.x + 1][p.y] == 0 && !visited[p.x + 1][p.y])
69+
resultList.add(new Point(p.x + 1, p.y, p.dist + 1));
70+
if(p.y > 0 && grid[p.x][p.y - 1] == 0 && !visited[p.x][p.y - 1])
71+
resultList.add(new Point(p.x, p.y - 1, p.dist + 1));
72+
if(p.y < m - 1 && grid[p.x][p.y + 1] == 0 && !visited[p.x][p.y + 1])
73+
resultList.add(new Point(p.x, p.y + 1, p.dist + 1));
8874

89-
class Point {
90-
int x, y, d;
91-
Point(int x, int y, int d) {
92-
this.x = x;
93-
this.y = y;
94-
this.d = d;
95-
}
75+
return resultList;
9676
}
9777

9878
public static void main(String args[]) {
99-
int[][] grid = {{1, 0, 2, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 1, 0, 0}};
79+
int[][] grid = {{1,1,1,1,1,0},{0,0,0,0,0,1},{0,1,1,0,0,1},{1,0,0,1,0,1},{1,0,1,0,0,1},{1,0,0,0,0,1},{0,1,1,1,1,0}};
10080
int[][] grid1 = {{1,1},{0,1}};
10181
ShortestDistanceFromAllBuildings shortestDistanceFromAllBuildings = new ShortestDistanceFromAllBuildings();
10282
System.out.println(shortestDistanceFromAllBuildings.shortestDistance(grid));

0 commit comments

Comments
 (0)