Skip to content

Add implementation for leetcode problems 733, 542#1408

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

Add implementation for leetcode problems 733, 542#1408
rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
rishigoswamy:master

Conversation

@rishigoswamy
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Flood Fill the image (leetcode_542.py)

It appears that you have submitted a solution for the "01 Matrix" problem (LeetCode 542) instead of the "Flood Fill" problem. The "Flood Fill" problem requires you to start from a specific pixel (sr, sc) and fill all connected pixels of the same color with a new color.

For the "Flood Fill" problem, you should use a BFS or DFS approach starting from (sr, sc). Here are the key steps:

  1. Check if the starting pixel already has the new color. If so, return the image immediately.
  2. Otherwise, remember the original color at (sr, sc).
  3. Use a queue (for BFS) or stack (for DFS) to traverse all connected pixels that have the original color.
  4. For each pixel, change its color to the new color and add its neighbors (up, down, left, right) that are within bounds and have the original color.

Your current solution for "01 Matrix" uses a multi-source BFS starting from all zeros, which is not applicable here. Please implement the correct algorithm for "Flood Fill".

VERDICT: NEEDS_IMPROVEMENT


Nearest zero (leetcode_733.py)

Your solution demonstrates a good understanding of BFS and flood fill, but it is for the wrong problem. Please carefully read the problem statement again. The problem is "Nearest Zero", which requires you to compute the distance from each cell to the nearest zero in the matrix.

For the correct problem, you should consider the following approach:

  • Initialize a queue and add all zeros to it, marking them as distance 0.
  • For non-zero cells, you can mark them as unvisited (e.g., with a negative value or a large number).
  • Then, perform a BFS from all zeros simultaneously, updating the distance for each neighbor.

You have the right idea with BFS, but the application is incorrect. Also, note that the problem constraints require an efficient solution since the matrix can be large.

Here is a hint: Instead of starting from a single point (like in flood fill), you need to start from all zeros at once. This is known as multi-source BFS.

VERDICT: NEEDS_IMPROVEMENT

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