LeetCode – Shortest Distance from All Buildings (Java)

Java Solution This problem can be solved by BFS. We define one matrix for tracking the distance from each building, and another matrix for tracking the number of buildings which can be reached. public int shortestDistance(int[][] grid) { int[][] distance = new int[grid.length][grid[0].length]; int[][] reach = new int[grid.length][grid[0].length];     int numBuilding = 0; for … Read more