nathanwailes

LeetCode 130 - Surrounded Regions - 2022.12.27 solution

Dec 26th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. class Solution:
  2.     def solve(self, board: List[List[str]]) -> None:
  3.         """
  4.        Do not return anything, modify board in-place instead.
  5.        How to solve it:
  6.        - The only way for a region to not be surrounded is if it is connected
  7.        to the edge of the board.  So we're going to run DFS from the edges of the
  8.        board to switch those regions to a temporary value, then iterate through
  9.        every cell to switch the remaining "O" cells to "X", and then iterate again
  10.        to switch the intermediate values back to "O".
  11.        """
  12.         ROWS = len(board)
  13.         COLS = len(board[0]) if ROWS else 0
  14.  
  15.         # Switch the regions connected to the edge to a temporary value
  16.  
  17.         top_edge = set([(0, i) for i in range(COLS)])
  18.         left_edge = set([(i, 0) for i in range(ROWS)])
  19.         right_edge = set([(i, COLS - 1) for i in range(ROWS)])
  20.         bottom_edge = set([(ROWS - 1, i) for i in range(COLS)])
  21.        
  22.         handled_positions = set()
  23.  
  24.         def flip_o_neighbors(pos):
  25.             adjustments = [
  26.                 [0, 1],
  27.                 [0, -1],
  28.                 [1, 0],
  29.                 [-1, 0],
  30.             ]
  31.             neighbors = [(a[0] + pos[0], a[1] + pos[1]) for a in adjustments]
  32.             for neighbor in neighbors:
  33.                 if neighbor[0] < 0 or neighbor[0] > ROWS - 1 or \
  34.                    neighbor[1] < 0 or neighbor[1] > COLS - 1:
  35.                    continue
  36.                 if board[neighbor[0]][neighbor[1]] == 'O':
  37.                     board[neighbor[0]][neighbor[1]] = 'T'
  38.                     handled_positions.add(neighbor)
  39.                     flip_o_neighbors(neighbor)
  40.  
  41.         for position in top_edge.union(left_edge).union(right_edge).union(bottom_edge):
  42.             if position not in handled_positions:
  43.                 if board[position[0]][position[1]] == 'O':
  44.                     board[position[0]][position[1]] = 'T'
  45.                     handled_positions.add(position)
  46.                     flip_o_neighbors(position)
  47.        
  48.  
  49.         # Switch the remaining "O" cells to "X"
  50.         for r in range(ROWS):
  51.             for c in range(COLS):
  52.                 if board[r][c] == 'O':
  53.                     board[r][c] = 'X'
  54.  
  55.         # Switch the intermediate values back to "O"
  56.         for r in range(ROWS):
  57.             for c in range(COLS):
  58.                 if board[r][c] == 'T':
  59.                     board[r][c] = 'O'
  60.        
  61.         return board
  62.  
Advertisement
Add Comment
Please, Sign In to add comment