221-- square buckle force maximum

This question is the use of dynamic programming, pay attention to good boundary conditions can be resolved.

The original title

In a two-dimensional matrix consisting of 0 and 1, to find the maximum square contains only 1, and returns its area.

Example:

输入: 

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

输出: 4

Original title url: https://leetcode-cn.com/problems/maximal-square/

Problem-solving

Dynamic Programming

This question should make us think very quickly using dynamic programming, from the upper left to the lower right corner to start looking.

Suppose we use a two-dimensional array dp, recording the largest square side length of each position can be formed (from the top left corner began to count). Position (i, j)is 1, it may constitute a side of the square is Min(dp(i - 1, j - 1), dp(i - 1, j), dp(i, j - 1)) + 1. See this, I believe you will understand, the largest side length of conditions for each position could constitute, in fact, is to require its top-left corner surrounded by three positions are 1 can.

Recurrence has been back to the original point, which means the first row and first column of the need to judge alone.

There should also be done before the optimization of space complexity is similar, we really need a true two-dimensional data dpto record intermediate results? In fact, we found that, when a position used, the digital location itself is no longer important, the key is the largest side of the square that position can be constructed, which is the intermediate results of our records. So you can directly update the numbers on the original array.

There is a need to consider some special cases, such as only one line or one, we should do.

Let's look at the code:

class Solution {
    public int maximalSquare(char[][] matrix) {
        // 高
        int height = matrix.length;
        if (height == 0) {
            return 0;
        }
        // 宽
        int width = matrix[0].length;

        // 检查第一行和第一列,是否有'1'
        int result = checkFirstRowAndCol(matrix, height, width);
        // 只有一行或一列,或者只有一个
        if (height == 1 || width == 1) {
            return result;
        }

        // result现在做为记录最大边的长度
        // 中间结果
        int temp;
        for (int i = 1; i < height; i++) {
            for (int j = 1; j < width; j++) {
                if (matrix[i][j] == '0') {
                    continue;
                }

                // 求出matrix[i - 1][j - 1]、matrix[i - 1][j]、matrix[i][j - 1]中的最小值
                temp = matrix[i - 1][j] < matrix[i][j - 1] ? matrix[i - 1][j] : matrix[i][j - 1];
                temp = temp < matrix[i - 1][j - 1] ? temp : matrix[i - 1][j - 1];
                // 更新当前节点的值
                matrix[i][j] = (char) (temp + 1);
                temp = temp + 1 - '0';

                if (result < temp) {
                    result = temp;
                }
            }
        }

        return result * result;
    }

    private int checkFirstRowAndCol(char[][] matrix, int height, int width) {
        // 检查第一列
        for (int i = 0; i < height; i++) {
            if (matrix[i][0] == '1') {
                return 1;
            }
        }

        // 检查第一行
        for (int i = 0; i < width; i++) {
            if (matrix[0][i] == '1') {
                return 1;
            }
        }

        return 0;
    }
}

Submit OK, when executed by: 5 msmemory 41.1 MBconsumption: .

This code above, I am not a one-time write out directly, and it is in constant submission and found some special cases not considered, fortunately force after completion of each deduction will submit will tell me when the input is not satisfied What kind of, but it also makes me often considered insufficient, still need to work.

to sum up

This question is above my answer process, I do not know if you understand. This question is the use of dynamic programming is actually almost the same, but the focus is still the special circumstances of judgment, need to pay attention.

Are interested can visit my blog or follow me number of public, headline number, maybe there will be surprises.

https://death00.github.io/

Public number: Jian Cheng Road

Guess you like

Origin www.cnblogs.com/death00/p/12173165.html