https://leetcode.com/problems/flood-fill/description/
Flood Fill - LeetCode
Flood Fill - An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[s
leetcode.com
EASY인데 엣지케이스땜에 생각보다 tricky했다.
내코드는 지저분해서 다른분 코드를 가져왔다.
class Solution:
def floodFill(self, image, sr, sc, newColor):
rows, cols, orig_color = len(image), len(image[0]), image[sr][sc]
def traverse(row, col):
if (not (0 <= row < rows and 0 <= col < cols)) or image[row][col] != orig_color:
return
image[row][col] = newColor
[traverse(row + x, col + y) for (x, y) in ((0, 1), (1, 0), (0, -1), (-1, 0))]
if orig_color != newColor:
traverse(sr, sc)
return image
이분은 그냥 엣지케이스를 따로 처리해주셨다.
'코딩 테스트 및 알고리즘 > leetcode for google' 카테고리의 다른 글
leetcode medium : Clone Graph (Grind 75 Graph 문제 3) (0) | 2023.02.24 |
---|---|
leetcode medium : 01 Matrix (Grind 75 Graph 문제 2) (0) | 2023.02.23 |
leetcode hard : Sum of Distances in Tree (Google 기출) (1) | 2023.02.20 |
leetcode medium : Merge Intervals (Facebook 코딩 인터뷰 기출) (0) | 2023.02.19 |
leetcode medium? : New 21 Game (0) | 2023.02.19 |