Maximal square

Find the largest square made only of 1s in a binary matrix. Each cell asks its three neighbours how big a square they end, and grows the smallest by one.

Time O(rows × cols)Space O(rows × cols)Code C#
Watch it animatedStep through it, with the running line highlighted. Free with an account.

The idea

Let dp[i, j] be the side of the biggest all-1 square whose bottom-right corner is cell (i, j). A square of side k ending here needs squares of side k - 1 ending directly above, directly to the left and diagonally up-left; the smallest of those three is the limit. So dp = 1 + min(up, left, up-left) on a 1, and 0 on a 0. The answer is the largest dp value, squared.

Code

public int MaximalSquare(char[][] matrix) {
    int rows = matrix.Length, cols = matrix[0].Length, best = 0;
    var dp = new int[rows + 1, cols + 1]; // extra 0 row and column: every cell has up, left and up-left
    for (int i = 1; i <= rows; i++)
        for (int j = 1; j <= cols; j++) {
            if (matrix[i - 1][j - 1] == '0') continue; // no square can end on a 0: dp stays 0
            dp[i, j] = 1 + Math.Min(dp[i - 1, j], Math.Min(dp[i, j - 1], dp[i - 1, j - 1]));
            best = Math.Max(best, dp[i, j]);
        }
    return best * best; // dp holds side lengths; the area is the side squared
}

Tips

In the .NET library

new int[rows + 1, cols + 1] is a rectangular array that the runtime fills with zeros, so the padding row and column cost no extra code. Index it as dp[i, j] (one bracket pair) rather than dp[i][j].