Solving Leetcode Interviews in Seconds with AI: Flood Fill
Introduction
In this blog post, we will explore how to solve the LeetCode problem "733" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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]. To perform a flood fill: Begin with the starting pixel and change its color to color. 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. 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. The process stops when there are no more adjacent pixels of the original color to update. Return the modified image after performing the flood fill. Example 1: Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2 Output: [[2,2,2],[2,2,0],[2,0,1]] 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. Example 2: Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0 Output: [[0,0,0],[0,0,0]] 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. Constraints: m == image.length n == image[i].length 1 <= m, n <= 50 0 <= image[i][j], color < 216 0 <= sr < m 0 <= sc < n
Explanation
- The core idea is to use Depth-First Search (DFS) to traverse all connected pixels that have the same color as the starting pixel and change their color to the new color.
- The DFS function recursively explores adjacent pixels (up, down, left, right) if they are within the image boundaries and have the same original color.
- The base case for the recursion is when the current pixel is out of bounds or has a different color than the original color, in which case the function returns without doing anything.
- Runtime Complexity: O(m*n), where m is the number of rows and n is the number of columns in the image. In the worst case, we might visit every pixel in the image.
- Storage Complexity: O(m*n) in the worst case, due to the recursive call stack of the DFS.
Code
def floodFill(image, sr, sc, color):
"""
Performs a flood fill on the image starting from the pixel image[sr][sc].
Args:
image: A 2D list of integers representing the image.
sr: The starting row index.
sc: The starting column index.
color: The new color to fill the connected pixels with.
Returns:
The modified image after performing the flood fill.
"""
rows = len(image)
cols = len(image[0])
start_color = image[sr][sc]
if start_color == color:
return image # No change needed
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or image[row][col] != start_color:
return
image[row][col] = color
# Explore adjacent pixels
dfs(row + 1, col) # Down
dfs(row - 1, col) # Up
dfs(row, col + 1) # Right
dfs(row, col - 1) # Left
dfs(sr, sc)
return image