Day 4: Matrix Traversal (flood fill)

Day 4: Matrix Traversal (flood fill)

flood fill


Flood Fill

Recommended : Solution

image = [[1,1,1],[1,1,0],[1,0,1]]
sr= 1
sc= 1
color = 2

def floodFill(image, sr, sc, color):
    # Get the starting color to replace
    start_color = image[sr][sc]
    
    # Define a nested function for depth-first search (DFS) to fill adjacent pixels
    def fill(x, y):
        # Check for out-of-bounds conditions
        if x < 0 or y < 0 or x >= len(image) or y >= len(image[0]):
            return
        
        # If the current pixel is already the target color or not the start color, return
        if image[x][y] == color or image[x][y] != start_color:
            return
        
        # Change the color of the current pixel to the target color
        image[x][y] = color
        
        # Define the four possible directions for movement (up, down, left, right)
        directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        
        # Recursively apply the fill function to adjacent pixels
        for dr, dc in directions:
            fill(x + dr, y + dc)
    
    # Start the fill operation from the starting pixel
    fill(sr, sc)
    
    # Return the modified image
    return image
        


  • Time Complexity: O(N×M)
  • Space Complexity: O(N×M) due to recursion stack

N and M are the number of rows and columns respectively


Victor Chiedo Ogbonna

Tech Innovator: Digital Marketing Specialist | Full-Stack Web Developer | Python & AI Researcher | Power BI Analyst

7 个月

Well said!

要查看或添加评论,请登录

Olaoluwa Saola的更多文章

  • Amazon Redshift Data Warehouse Project Using S3, Boto3, and psycopg2

    Amazon Redshift Data Warehouse Project Using S3, Boto3, and psycopg2

    In this project, I worked with Amazon Redshift and S3 to build a data warehouse solution, automating data ingestion and…

    2 条评论
  • Data Warehouse part 2: OLAP vs OLTP

    Data Warehouse part 2: OLAP vs OLTP

    Data engineering plays a pivotal role in modern data management, enabling organizations to efficiently process and…

    2 条评论
  • Building a Data Warehouse Part 1: A Step-by-Step Guide

    Building a Data Warehouse Part 1: A Step-by-Step Guide

    In today’s data-driven world, the ability to effectively store, manage, and analyze large volumes of data is crucial…

    8 条评论
  • Data Engineering 101: Setting Up PostgreSQL with Python

    Data Engineering 101: Setting Up PostgreSQL with Python

    Read article on Medium Data engineering involves managing and processing data for analysis, which often starts with…

    4 条评论
  • Day 20: Find Median of a Datastream (Two Heaps)

    Day 20: Find Median of a Datastream (Two Heaps)

    Median of a Datastream Recommended: Solution The median is the middle value in an ordered integer list. If the size of…

  • Day 19: Modified Binary Search (Search Insert Position)

    Day 19: Modified Binary Search (Search Insert Position)

    Search Insert Position Recommended: Solution Given a sorted array of distinct integers and a target value, return the…

  • Day 18 Missing Number (Cyclic Sort)

    Day 18 Missing Number (Cyclic Sort)

    Missing Number Recommended: Solution Given an array containing distinct numbers in the range , return the only number…

    2 条评论
  • Day 17: Path Sum II (DFS)

    Day 17: Path Sum II (DFS)

    Path Sum II Recommended: Solution Given the of a binary tree and an integer , return all root-to-leaf paths where the…

  • Day 16: Path Sum (Tree Depth First Search)

    Day 16: Path Sum (Tree Depth First Search)

    Path Sum Recommended: Solution Given the of a binary tree and an integer , return if the tree has a root-to-leaf path…

    1 条评论
  • Recap

    Recap

    ?? Here are the links to my previous posts: Day 15: Zig zag Binary Tree Traversal Day 14: Reverse Level order traversal…

    4 条评论

社区洞察

其他会员也浏览了