data-structures · intermediate · ~15 min
Find the largest all-ones square in a binary matrix.
Implement:
int maximal_square(const int *grid, int rows, int cols);
Return the side length of the largest square consisting entirely of 1s in a rows x cols 0/1 matrix (row-major).
Row-major 0/1 grid, dimensions.
Side length of the largest all-1 square.
dp[i][j] is the side of the largest square ending at (i,j).
#include <stddef.h>
/* Side length of the largest all-1 square in a rows x cols 0/1 matrix (row-major). */
int maximal_square(const int *grid,int rows,int cols){ (void)grid;(void)rows;(void)cols; return 0; }
Taking the max instead of the min of the three neighbours (that overcounts).
All zeros -> 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.