Advertisement
1988coder

317. Shortest Distance from All Buildings

Nov 30th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. /*
  2. BFS for each building. Use reach 2D array to skip non reachable cells.
  3. This solution does not modify the input grid.
  4.  
  5. Time Complexity:
  6. = M*N + 1s * 0s
  7. = M*N + K*(M*N - K)   (if K = number of buildings (1s))
  8. = M*N + K*M*N - K^2
  9. = O(K*M*N)
  10.  
  11. In worst case K = (M*N)/4...
  12. For example.. there are M/2 rows that have buildings.. and each row has N/2 buildings
  13. B E B E B E
  14. E E E E E E
  15. B E B E B E
  16. E E E E E E
  17. B E B E B E
  18. E E E E E E
  19. Thus worst case Time Complexity will be O((M^2) * (N^2))
  20.  
  21. Space Complexity: O(M * N)
  22. M = Number of rows
  23. N = Number of columns
  24. K = Number of Buildings (1s)
  25. */
  26. class Solution {
  27.     private static final int[][] dirs = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  28.    
  29.     public int shortestDistance(int[][] grid) {
  30.         if (grid == null || grid.length == 0 || grid[0].length == 0) {
  31.             return -1;
  32.         }
  33.        
  34.         int rows = grid.length;
  35.         int cols = grid[0].length;
  36.        
  37.         int[][] reach = new int[rows][cols];
  38.         int[][] dist = new int[rows][cols];
  39.         int minDist = Integer.MAX_VALUE;
  40.         int bldg = 0;
  41.        
  42.         for (int i = 0; i < rows; i++) {
  43.             for (int j = 0; j < cols; j++) {
  44.                 if (grid[i][j] == 1) {
  45.                     minDist = bfsHelper(grid, reach, dist, i, j, bldg);
  46.                     if (minDist == Integer.MAX_VALUE) {
  47.                         return -1;
  48.                     }
  49.                     bldg++;
  50.                 }
  51.             }
  52.         }
  53.        
  54.         return minDist == Integer.MAX_VALUE ? -1 : minDist;
  55.     }
  56.    
  57.     private int bfsHelper(int[][] grid, int[][] reach, int[][] dist, int row, int col, int bldg) {
  58.         int minDist = Integer.MAX_VALUE;
  59.         int level = 1;
  60.        
  61.         Queue<int[]> queue = new LinkedList();
  62.         queue.offer(new int[]{row, col});
  63.        
  64.         while (!queue.isEmpty()) {
  65.             int qSize = queue.size();
  66.             for (int q = 0; q < qSize; q++) {
  67.                 int[] cell = queue.poll();
  68.  
  69.                 for (int[] dir : dirs) {
  70.                     int x = cell[0] + dir[0];
  71.                     int y = cell[1] + dir[1];
  72.  
  73.                     if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] != 0 || reach[x][y] != bldg) {
  74.                         continue;
  75.                     }
  76.  
  77.                     reach[x][y] = bldg+1;
  78.                     dist[x][y] += level;
  79.                     minDist = Math.min(minDist, dist[x][y]);
  80.  
  81.                     queue.offer(new int[]{x,y});
  82.                 }
  83.             }
  84.             level++;
  85.         }
  86.        
  87.         return minDist;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement