|
| 1 | +{ |
| 2 | + "problem_name": "flood_fill", |
| 3 | + "solution_class_name": "Solution", |
| 4 | + "problem_number": "733", |
| 5 | + "problem_title": "Flood Fill", |
| 6 | + "difficulty": "Easy", |
| 7 | + "topics": "Array, Depth-First Search, Breadth-First Search, Matrix", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "You are given an image represented by an `m x n` grid of integers `image`, where `image[i][j]` represents the pixel value of the image. You are also given three integers `sr`, `sc`, and `color`. Your task is to perform a **flood fill** on the image starting from the pixel `image[sr][sc]`.\n\nTo perform a **flood fill**:\n\n1. Begin with the starting pixel and change its color to `color`.\n2. Perform the same process for each pixel that is **directly adjacent** (pixels that share a side with the original pixel, either horizontally or vertically) and shares the **same color** as the starting pixel.\n3. Keep **repeating** this process by checking neighboring pixels of the *updated* pixels and modifying their color if it matches the original color of the starting pixel.\n4. The process **stops** when there are **no more** adjacent pixels of the original color to update.\n\nReturn the **modified** image after performing the flood fill.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "\n\n```\nInput: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2\nOutput: [[2,2,2],[2,2,0],[2,0,1]]\n```\n**Explanation:** From the center of the image with position `(sr, sc) = (1, 1)` (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color. Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel." |
| 13 | + }, |
| 14 | + { |
| 15 | + "content": "```\nInput: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0\nOutput: [[0,0,0],[0,0,0]]\n```\n**Explanation:** The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image." |
| 16 | + } |
| 17 | + ], |
| 18 | + "readme_constraints": "- `m == image.length`\n- `n == image[i].length`\n- `1 <= m, n <= 50`\n- `0 <= image[i][j], color < 2^16`\n- `0 <= sr < m`\n- `0 <= sc < n`", |
| 19 | + "readme_additional": "", |
| 20 | + "solution_imports": "", |
| 21 | + "solution_methods": [ |
| 22 | + { |
| 23 | + "name": "flood_fill", |
| 24 | + "parameters": "image: list[list[int]], sr: int, sc: int, color: int", |
| 25 | + "return_type": "list[list[int]]", |
| 26 | + "dummy_return": "[]" |
| 27 | + } |
| 28 | + ], |
| 29 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution", |
| 30 | + "test_class_name": "FloodFill", |
| 31 | + "test_helper_methods": [ |
| 32 | + { "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" } |
| 33 | + ], |
| 34 | + "test_methods": [ |
| 35 | + { |
| 36 | + "name": "test_flood_fill", |
| 37 | + "parametrize": "image, sr, sc, color, expected", |
| 38 | + "parametrize_typed": "image: list[list[int]], sr: int, sc: int, color: int, expected: list[list[int]]", |
| 39 | + "test_cases": "[([[1, 1, 1], [1, 1, 0], [1, 0, 1]], 1, 1, 2, [[2, 2, 2], [2, 2, 0], [2, 0, 1]]), ([[0, 0, 0], [0, 0, 0]], 0, 0, 0, [[0, 0, 0], [0, 0, 0]]), ([[0, 0, 0], [0, 1, 1]], 1, 1, 1, [[0, 0, 0], [0, 1, 1]]), ([[1, 1, 1], [1, 1, 0], [1, 0, 1]], 1, 1, 1, [[1, 1, 1], [1, 1, 0], [1, 0, 1]])]", |
| 40 | + "body": "result = self.solution.flood_fill(image, sr, sc, color)\nassert result == expected" |
| 41 | + } |
| 42 | + ], |
| 43 | + "playground_imports": "from solution import Solution", |
| 44 | + "playground_test_case": "# Example test case\nimage = [[1, 1, 1], [1, 1, 0], [1, 0, 1]]\nsr = 1\nsc = 1\ncolor = 2\nexpected = [[2, 2, 2], [2, 2, 0], [2, 0, 1]]", |
| 45 | + "playground_execution": "result = Solution().flood_fill(image, sr, sc, color)\nresult", |
| 46 | + "playground_assertion": "assert result == expected" |
| 47 | +} |
0 commit comments