Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    [LeetCode 130] Surrounded Regions

    Published Mar 28, 2022 [  Graph  ]

    Problem

    Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by ‘X’.

    A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.

    Example 1:

    Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
    Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
    Explanation: Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
    

    Example 2:

    Input: board = [["X"]]
    Output: [["X"]]
    

    Constraints:

    • m == board.length
    • n == board[i].length
    • 1 <= m, n <= 200
    • board[i][j] is 'X' or 'O'.

    TypeScript

    /**
     Do not return anything, modify board in-place instead.
     */
    function solve(board: string[][]): void {
        const ROWS: number = board.length;
        const COLS: number = board[0].length;
    
        // DFS to mark O to T
        const capture = (row: number, col: number): void => {
            if(row < 0 || col < 0 || row >= ROWS || col >= COLS || board[row][col] !== 'O'){
                return;
            }
            board[row][col] = 'T'
            capture(row, col - 1)
            capture(row - 1, col)
            capture(row, col + 1)
            capture(row + 1, col)
        }
    
        // run capture for all boarder Os
        for(let row = 0; row < ROWS; row++){
            for(let col = 0; col < COLS; col++){
                if((row === 0 || col === 0 || row === ROWS - 1 || col === COLS - 1) && (board[row][col] === 'O')){
                    capture(row, col)
                }
            }
        }
    
        // convert all remaining Os to Xs
        for(let row = 0; row < ROWS; row++){
            for(let col = 0; col < COLS; col++){
                if(board[row][col] === 'O'){
                    board[row][col] = 'X'
                }
            }
        }
    
        // convert all Ts to Os
        for(let row = 0; row < ROWS; row++){
            for(let col = 0; col < COLS; col++){
                if(board[row][col] === 'T'){
                    board[row][col] = 'O'
                }
            }
        }
    };
    

    Reference