Day 97 | Surrounded Regions

Day 97 | Surrounded Regions

In this problem, you are given a 2-D grid. This grid contains "X" an "O" characters. If a group of O's is surrounded by X's on all 4 sides, it is considered to be surrounded. The goal of this problem is to change all surrounded regions of O's to X's. This is a pretty interesting problem that I learned a lot from.

The Approach

Initially, I thought of starting from all the O's on the board. I asked myself, how can I look for O's that are surrounded on the board. I initially started by thinking that I can traverse the board, look for O's, then perform a depth first search to see if it's group was surounded by X's. This approach felt a little complicated. The reason why is that you're not working with a singular cell. You are working with a group of cells. It's a lot more difficult to keep track if a whole group of cell's is surrounded rather than a single cell.

I actually found a different approach. What if instead you look for the group of cells that aren't surrounded. It's actually quite easy to find which cells these are. They are all the group of O's that are on the border of the grid. We can traverse all the borders, look for O's, perform a BFS on the cells surrounding those O's to mark those group of O's, then traverse the entire grid again to look for groups of O's that aren't marked. These groups that aren't marked are surrounded. We turn those to X's.

The Code

class Solution:
    def solve(self, board: List[List[str]]) -> None:
        ROWS, COLS = len(board), len(board[0])
        q = deque()
        for r in range(ROWS):
            for c in range(COLS):
                if (r == 0 or r == ROWS - 1 or c == 0 or c == COLS-1) and board[r][c] == "O":
                    q.append((r,c))
        while(q):
            for _ in range(len(q)):
                (r,c) = q.popleft()
                print(q)
                board[r][c] = "I"
                directions = [[1,0],[0,1],[-1,0],[0,-1]]
                for rowAdd, colAdd in directions:
                    new_r, new_c = r + rowAdd, c + colAdd
                    if new_r >= 0 and new_c >= 0 and new_r < ROWS and new_c < COLS and board[new_r][new_c] == "O":
                        print(board[new_r][new_c])
                        q.append((new_r,new_c))
        for r in range(ROWS):
            for c in range(COLS):
                if board[r][c] == "O":
                    board[r][c] = "X"
                if board[r][c] == "I":
                    board[r][c] = "O"
            
                                 

We start by creating a queue. We then loop through the entire grid. If the current cell is on the border, and is an O, we then add it to the queue. We then perform a BFS on those cells. We mark all the border group of O's with and 'I'. This way we keep track of the cells that are not surrounded. We then loop through the entire grid. If the current cell is an O, we change it to an X. If the current cell is an I, we change it back to and O.

Complexity Analysis

The time complexity of this solution is going to be O(m x n). The reason why is that the bfs method can only process each cell at most once. We then loop through the entire board another 2 times. This gives us something like O((m x n) + (m x n) + (m x n)) which just simplifies to m x n.

The space complexity is proportional to the size of the grid. This is because worst case scenario, the entire board is made up of O's which mean that every single cell would be in the queue at some point.

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

Haroon Almadani的更多文章

  • Day 98 | Course Schedule

    Day 98 | Course Schedule

    Today's problem was a little different from the other graph problems I've solved so far. While usually I get a grid or…

  • Day 96 | Pacific Atlantic Water Flow

    Day 96 | Pacific Atlantic Water Flow

    Todays graph problem took me a little bit of time to understand. You are given a rectangular island heights where each…

  • Day 95 | Rotten Oranges

    Day 95 | Rotten Oranges

    Today I solved a problem that was practically the same as yesterdays problem with a little variation. You are given an…

  • Day 94 | Islands and Treasure

    Day 94 | Islands and Treasure

    Today I solved a problem involving a matrix again. You are given a m x n matrix that can consist of only 3 possible…

  • Day 93 | Clone Graph

    Day 93 | Clone Graph

    The last two graph problems I've solved involved an adjacency matrix representation of a graph. This problem was a…

  • Day 92 | Max Area of Islands

    Day 92 | Max Area of Islands

    Today's problem heavily built off yesterdays problem. I recommend solving number of islands first before tackling this…

  • Day 91 | Number of Islands

    Day 91 | Number of Islands

    Today I've moved on from backtracking problems. I'm onto graph problems! This problem involved counting the number of…

  • Day 90 | Letter Combinations of a Phone Number

    Day 90 | Letter Combinations of a Phone Number

    Today's problem was a pretty fun problem. It feels like it's combined everything I've learned in the past couple of…

  • Day 89 | Palindrome Partitioning

    Day 89 | Palindrome Partitioning

    Palindrome Partitioning was a problem that I initially had troubles wrapping my head around. Most backtracking problems…

  • Day 88 | Word Search

    Day 88 | Word Search

    Today I solved a pretty fun problem. I was given a 2-d grid of characters and I was to find out if a word was present…

社区洞察

其他会员也浏览了