알고리즘/LeetCode
LeetCode 풀기 - 733. Flood Fill
앤테바
2021. 11. 17. 05:29
반응형
733. Flood Fill
https://leetcode.com/problems/flood-fill/
Flood Fill - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1)
DFS 방법을 사용합니다.
상하좌우를 이동해가면서 old_color를 new_color로 변경해나갑니다.
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
old_color = image[sr][sc]
max_row = len(image)
max_col = len(image[0])
def dfs(row, col):
# Check image boundary
if not (0 <= row < max_row and 0 <= col < max_col):
return
if image[row][col] != old_color:
return
image[row][col] = newColor
dfs(row - 1, col)
dfs(row + 1, col)
dfs(row, col - 1)
dfs(row, col + 1)
if old_color != newColor:
dfs(sr, sc)
return image
반응형