Skip to content

Adding all the solutions#1397

Open
subbu4061 wants to merge 1 commit intosuper30admin:masterfrom
subbu4061:master
Open

Adding all the solutions#1397
subbu4061 wants to merge 1 commit intosuper30admin:masterfrom
subbu4061:master

Conversation

@subbu4061
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You provided two correct solutions (DFS and BFS) for the Flood Fill problem, showing understanding of different approaches.
  • You included comments explaining the time and space complexity for each solution.
  • You handled the base case where the starting pixel is already the target color.

Areas for Improvement:

  1. Remove unrelated code: The solution for "01Matrix" should not be included in the same file. Each problem should have its own solution file to avoid confusion.
  2. Avoid redundant operations: In the DFS solution, inside the loop, you set image[r][c] = color before the recursive call. This is redundant because the recursive call will set it again. You can remove this line and just call dfs(r, c, image, color, initColor) without setting the color first.
  3. Consider using enhanced for loops: Instead of using a for loop with index i, you can use an enhanced for loop to iterate over dirs for better readability.
  4. Consistency: The BFS solution is commented out, which is fine for showing alternatives, but ensure that the submitted solution is the one you intend to be evaluated.

Suggested improved DFS code:

class Solution {
    int[][] dirs = {{0,-1}, {0,1}, {1,0}, {-1,0}};
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        if (image[sr][sc] == color) return image;
        dfs(sr, sc, image, color, image[sr][sc]);
        return image;     
    }

    private void dfs(int sr, int sc, int[][] image, int color, int initColor) {
        image[sr][sc] = color;
        for (int[] dir : dirs) {
            int r = sr + dir[0];
            int c = sc + dir[1];
            if (r >= 0 && r < image.length && c >= 0 && c < image[0].length) {
                if (image[r][c] == initColor) {
                    dfs(r, c, image, color, initColor);
                }
            }
        }
    }
}

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You provided two correct solutions (DFS and BFS) for the Flood Fill problem, demonstrating understanding of both approaches.
  • The code is well-commented with explanations of time and space complexity.
  • You correctly handle the base case where the starting pixel is already the target color.

Areas for Improvement:

  • Remove unrelated code. The solution for "01Matrix" should not be included in the Flood Fill solution. This makes the submission confusing.
  • In the DFS solution, the assignment image[r][c] = color; inside the for loop is unnecessary because the recursive call to dfs will set the color. You can remove that line to avoid redundant operations.
  • Consider using the BFS approach for larger grids to avoid potential stack overflow, though for the given constraints DFS is acceptable.
  • For the BFS solution, you are using a queue of integers and adding two integers per pixel. This is fine, but you could also use a queue of pairs (e.g., using an array of length 2) for better readability.

Overall, your solutions are correct and efficient. Just ensure that you only submit the relevant code for the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants