[Coding Made Simple] Maximum Subsquare surrounded by 'X'

Given a 2D matrix where every element is either ‘O’ or ‘X’, find the largest subsquare surrounded by ‘X’.

Examples:

Input: mat[N][N] = { {‘X‘, ‘O‘, ‘X‘, ‘X‘, ‘X‘},
                     {‘X‘, ‘X‘, ‘X‘, ‘X‘, ‘X‘},
                     {‘X‘, ‘X‘, ‘O‘, ‘X‘, ‘O‘},
                     {‘X‘, ‘X‘, ‘X‘, ‘X‘, ‘X‘},
                     {‘X‘, ‘X‘, ‘X‘, ‘O‘, ‘O‘},
                    };
Output: 3
The square submatrix starting at (1, 1) is the largest
submatrix surrounded by ‘X‘

Input: mat[M][N] = { {‘X‘, ‘O‘, ‘X‘, ‘X‘, ‘X‘, ‘X‘},
                     {‘X‘, ‘O‘, ‘X‘, ‘X‘, ‘O‘, ‘X‘},
                     {‘X‘, ‘X‘, ‘X‘, ‘O‘, ‘O‘, ‘X‘},
                     {‘X‘, ‘X‘, ‘X‘, ‘X‘, ‘X‘, ‘X‘},
                     {‘X‘, ‘X‘, ‘X‘, ‘O‘, ‘X‘, ‘O‘},
                    };
Output: 4
The square submatrix starting at (0, 2) is the largest
submatrix surrounded by ‘X‘

Solution 1. O(N^4) runtime.

Consider every square submatrix and check whether it is only surrounded by ‘X‘.

There are O(N^3) square submatrices and each check takes O(N) time.

Solution 2. O(N^3) runtime, O(N^2) space, using dynamic programming

The idea is to create two auxiliary arrays hor[N][N] and ver[N][N].

The value stored in hor[i][j] is the number of horizontal continuous ‘X’ characters from left to right till matrix[i][j] in matrix[][].

Similarly, the value stored in ver[i][j] is the number of vertical continuous ‘X’ characters from top to bottom till matrix[i][j] in matrix[][].

Once we have filled values in hor[][] and ver[][], we start from the bottommost-rightmost corner of matrix and move toward the leftmost-topmost in row by row manner.

For every matrix[i][j] that is ‘X‘, we compare the values of hor[i][j] and ver[i][j], and pick the smaller of two as we need a square. Let the smaller of two be ‘small’.

At this point, we are sure that there is a right vertical line and bottom horizontal line of length at least ‘small‘.

We find a bigger square if the following conditions are met.

1. small is bigger than the current max size length;

2. there is a left vertical line of length >= ‘small‘;

3. there is a top horizontal line of length >= ‘small‘.

If the side length of ‘small‘ does not generate a square, we try for small - 1. Repeat this until small is not bigger than max anymore.

 1 public class MaxSquareSubmatrix {
 2     public int getMaxSquareSubmatrixSideX(int[][] matrix) {
 3         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
 4             return 0;
 5         }
 6         int[][] hor = new int[matrix.length][matrix[0].length];
 7         int[][] ver = new int[matrix.length][matrix[0].length];
 8         hor[0][0] = (matrix[0][0] == ‘X‘ ? 1 : 0);
 9         ver[0][0] = (matrix[0][0] == ‘X‘ ? 1 : 0);
10         for(int i = 1; i < matrix.length; i++) {
11             if(matrix[i][0] == ‘O‘) {
12                 ver[i][0] = 0;
13                 hor[i][0] = 0;
14             }
15             else {
16                 ver[i][0] = 1 + ver[i - 1][0];
17                 hor[i][0] = 1;
18             }
19         }
20         for(int j = 1; j < matrix[0].length; j++) {
21             if(matrix[0][j] == ‘O‘) {
22                 hor[0][j] = 0;
23                 ver[0][j] = 0;
24             }
25             else {
26                 hor[0][j] = 1 + hor[0][j - 1];
27                 ver[0][j] = 1;
28             }
29         }
30         for(int i = 1; i < matrix.length; i++) {
31             for(int j = 1; j < matrix[0].length; j++) {
32                 if(matrix[i][j] == ‘O‘) {
33                     hor[i][j] = 0;
34                     ver[i][j] = 0;
35                 }
36                 else {
37                     hor[i][j] = 1 + hor[i][j - 1];
38                     ver[i][j] = 1 + ver[i - 1][j];
39                 }
40             }
41         }
42         int max = 0;
43         for(int i = matrix.length - 1; i >= 0; i--) {
44             for(int j = matrix[0].length - 1; j >= 0; j--) {
45                 if(matrix[i][j] == ‘X‘) {
46                     int small = Math.min(hor[i][j], ver[i][j]);
47                     while(small > max) {
48                         if(ver[i][j - small + 1] >= small && hor[i - small + 1][j] >= small) {
49                             max = small;
50                             break;
51                         }
52                         else {
53                             small--;
54                         }
55                     }
56                 }
57             }
58         }
59         return max;
60     }
61 }

Related Problems

Maximal Square

Maximal Square II

[Coding Made Simple] Maximum Subsquare surrounded by 'X'

时间: 2024-12-22 11:48:43

[Coding Made Simple] Maximum Subsquare surrounded by 'X'的相关文章

[Coding Made Simple] Maximum Sum Subsequence Non-adjacent

Given an array of positive number, find maximum sum subsequence such that elements in this subsequence are not adjacent to each other. Recursive formula: f(n) = Math.max{f(n - 1), f(n - 2) + arr[n - 1]}. Dynamic programming is used to get rid of the

[Coding Made Simple] Box Stacking

Given boxes of different dimensions, stack them on top of each other to get maximum height such that box on top has strictly less length and width than box under it. Algorithm. 1. get all the box permutation with each permutation's length >= width; s

[CareerCup] 18.11 Maximum Subsquare 最大子方形

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. LeetCode上的原题,请参见我之前的解法Maximal Square.书上给了两种解法,但是比较长:

[Coding Made Simple] Burst Balloon

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r

[Coding Made Simple] Matrix Chain Multiplication

Given some matrices, in what order you would multiply them to minimize cost of multiplication. The following problem formulation is extracted from this link. Problem Formulation Note that although we can use any legal parenthesization, which will lea

[Coding Made Simple] Sum Query in 2D Immutable Array

Given a 2D immutable array,  Write an efficient program to support any given sub-rectangle sum query in this 2D matrix. A simple solution is to add each entry inside the sub-rectangle. The runtime is O(n * m). The problem of this solution is that for

[Coding Made Simple] Optimal Strategy Game Pick from Ends of array

N pots, each with some number of gold coins, are arranged in a line. You are playing a game against another player. You take turns picking a pot of gold. You may pick a pot from either end of the line, remove the pot, and keep the gold pieces. The pl

[Coding Made Simple] Minimum Cost Path

Given a 2 dimensional matrix, find minimum cost path to reach bottom right from top left provided you can only from down and right. For a 5 * 5 matrix, think of solving this problem by breaking it into smaller subproblems. And overlapping subproblems

[Coding Made Simple] Number without consecutive 1s in binary representation

Given a number n, find the total number of numbers from 0 to 2^n - 1 which do not have consecutive 1s in their binary representation. Solution.  This problem is the equivalence of fibonacci sequence. For a given n, f(n) = f(n - 1) + f(n - 2). 1 publi