221. Maximal Square

yPhantom 2019年10月14日 24次浏览

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Example:

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Output: 4

Solution

看到这种类似的题,想动态规划或者DFS/BFS。

本题有一个转移方程:dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1

解法一

class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int m = matrix.length;
        int n = matrix[0].length;

        int[][] dp = new int[m][n];
        int maxEdge = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || j == 0 || matrix[i][j] == '0') {
                    dp[i][j] = matrix[i][j] - '0';
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
                }
                maxEdge = Math.max(maxEdge, dp[i][j]);
            }
        }
        return maxEdge * maxEdge;
    }
}

解法二

解法一是最容易想到的方法,此时的时间复杂度是O(n2),空间复杂度是O(mn)。我们可以对空间复杂度进行优化,实际上转移方程仅仅需要两行即可。可以改成这样:

class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int m = matrix.length;
        int n = matrix[0].length;

        int[] pre = new int[n];
        int[] cur = new int[n];
        int maxEdge = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || j == 0 || matrix[i][j] == '0') {
                    cur[j] = matrix[i][j] - '0';
                } else {
                    cur[j] = Math.min(pre[j - 1], Math.min(pre[j], cur[j - 1])) + 1;
                }
                maxEdge = Math.max(maxEdge, cur[j]);
            }
            int[] tmp = pre;
            pre = cur;
            cur = tmp;
        }
        return maxEdge * maxEdge;
    }
}

此时空间复杂度是O(2n)

解法三

甚至我们可以将O(2n)再减小到O(n)。

class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        int pre = 0;

        int[] cur = new int[n];
        int maxEdge = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int tmp = cur[j];
                if (i == 0 || j == 0 || matrix[i][j] == '0') {
                    cur[j] = matrix[i][j] - '0';
                } else {
                    cur[j] = Math.min(pre, Math.min(cur[j], cur[j - 1])) + 1;
                }
                maxEdge = Math.max(maxEdge, cur[j]);
                pre = tmp;
            }
        }
        return maxEdge * maxEdge;
    }
}

在每一个j循环的过程中,pre就代表了上一行cur[j],在j+1时使用,即代表dp[i-1][j-1]。而cur[j]代表dp[i-1][j],cur[j-1]代表dp[i][j-1]