[Programming Problem] Surrounded Regions

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”]]

[Problem Link]

The high level idea here is to traverse the borders and run the floodfill algorithm to identify regions touching borders. Once we’ve identified those, we’ve identified regions we should leave alone vs. regions we need to flip. To do this we:-

  1. Make a copy of the board
  2. Run floodfill algorithm on: top and bottom row, first and last column
  3. After floodfill algorithm, all border islands will be marked as ‘-‘
  4. We then traverse our (copied) board to set appropriate values on board (depending on output from floodfill algorithm). We set all border islands (depicted by ‘-‘) as ‘O’ and inner islands (depicted by ‘O’) set as ‘X’
/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solve = function(board) {
    // make a copy of the board
    const boardCopy = board.map((x) => x.map((y) => y));
 
    // Run floodfill algorithm on: top row and botton row
    for ( let j = 0 ; j < boardCopy[0].length ; j++ ) {
        if (boardCopy[0][j] === 'O') floodFill(boardCopy, 0, j);
        if (boardCopy[boardCopy.length-1][j] === 'O') floodFill(boardCopy, boardCopy.length-1, j);
    }
 
    // Run floodfill algorithm on: first and last column
    for ( let i = 0 ; i < boardCopy.length ; i++ ) {
        if (boardCopy[i][0] === 'O') floodFill(boardCopy, i, 0);
        if (boardCopy[i][boardCopy[0].length-1] === 'O') floodFill(boardCopy, i, boardCopy[0].length-1);
    }
 
    // After floodfill algorithm, all border islands will be marked as '-'
    // We basically set all border islands as 'O'
    // Other values with 'O' (which are inner islands) will be set as 'X'    
    // Set appropriate values on board (depending on output from floodfill algorithm)
    boardCopy.forEach((x, i) => {
        return x.forEach((y, j) => {
            if (y === '-') {
                board[i][j] = 'O';
            } else if (y === 'O') {
                board[i][j] = 'X'
            } else { board[i][j] = y };
        })
    })
 
    return board;
};
 
var floodFill = function(board, i, j) {
    if ( i < 0 || i >= board.length || j < 0 || j >= board[0].length ) return;
 
    if (board[i][j] === 'O') {
        board[i][j] = '-';
        floodFill(board, i+1, j);
        floodFill(board, i-1, j);
        floodFill(board, i, j+1);
        floodFill(board, i, j-1);        
    }
}
Software Everyday – Just another WordPress site

[Programming Problem] Subsets II

Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets....

[Programming Problem] Flatten Binary Tree to Linked List

Given the root of a binary tree, flatten the tree into a “linked list”: – The “linked list” should use the same TreeNode class...

[Programming Problem] Largest Number

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.   Since the result may...

[Programming Problem] Sum Root to Leaf Numbers

You are given the root of a binary tree containing digits from 0 to 9 only.   Each root-to-leaf path in the tree represents...

[Programming Problem] Reverse Linked List II

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list...

[Programming Problem] Jump Game II

Given an array of non-negative integers nums, you are initially positioned at the first index of the array. Each element in the array represents...

[Programming Problem] Find Minimum in Rotated Sorted Array II

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7]...

[Programming Problem] Find Minimum in Rotated Sorted Array

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7]...

[Programming Problem] Print FooBar Alternately

Suppose you are given the following code: class FooBar { public void foo() { for (int i = 0; i < n; i++) {...

[Programming Problem] The Dining Philosophers

[Problem Link] Solution here is to add some `ordering` on the resources (i.e. forks) requested by the processes (i.e. the philosophers). Identify the resource...