Advertisement
1988coder

733. Flood Fill

Dec 9th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. /*
  2. BFS Solution
  3.  
  4. Time Complexity: O(M * N)
  5. Space Complexity: O(M * N) (queue size can grow upto all pixels)
  6.  
  7. M = Number of rows
  8. N = Number of columns.
  9. */
  10. class Solution {
  11.     private static final int[][] dirs = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
  12.    
  13.     public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
  14.         if (image == null || image.length == 0 || image[0].length == 0) {
  15.             return new int[0][0];
  16.         }
  17.         int rows = image.length;
  18.         int cols = image[0].length;
  19.         if (sr < 0 || sr >= rows || sc < 0 || sc >= cols || image[sr][sc] == newColor) {
  20.             return image;
  21.         }
  22.        
  23.         Queue<int[]> queue = new LinkedList();
  24.         int oldColor = image[sr][sc];
  25.         image[sr][sc] = newColor;
  26.         queue.add(new int[]{sr, sc});
  27.        
  28.         while (!queue.isEmpty()) {
  29.             int[] cell = queue.poll();
  30.             for (int[] dir : dirs) {
  31.                 int x = cell[0] + dir[0];
  32.                 int y = cell[1] + dir[1];
  33.                 if (x < 0 || y < 0 || x >= rows || y >= cols || image[x][y] != oldColor) {
  34.                     continue;
  35.                 }
  36.                 image[x][y] = newColor;
  37.                 queue.add(new int[]{x, y});
  38.             }
  39.         }
  40.        
  41.         return image;
  42.     }
  43. }
  44.  
  45. /*
  46. DFS Solution
  47.  
  48. Time Complexity: O(M * N)
  49. Space Complexity: O(M * N) (recursion stack size can grow upto all pixels)
  50.  
  51. M = Number of rows
  52. N = Number of columns.
  53. */
  54. class Solution {
  55.     public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
  56.         if (image == null || image.length == 0 || image[0].length == 0) {
  57.             return new int[0][0];
  58.         }
  59.         if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] == newColor) {
  60.             return image;
  61.         }
  62.        
  63.         int oldColor = image[sr][sc];  
  64.         dfs(image, sr, sc, oldColor, newColor);
  65.         return image;
  66.     }
  67.    
  68.     private void dfs(int[][] image, int row, int col, int oldColor, int newColor) {
  69.         if (row < 0 || col < 0 || row >= image.length || col >= image[0].length || image[row][col] != oldColor) {
  70.             return;
  71.         }
  72.         image[row][col] = newColor;
  73.         dfs(image, row, col+1, oldColor, newColor);
  74.         dfs(image, row+1, col, oldColor, newColor);
  75.         dfs(image, row, col-1, oldColor, newColor);
  76.         dfs(image, row-1, col, oldColor, newColor);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement