Skip to content

Commit d895c2b

Browse files
Maximal Square
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent a000d94 commit d895c2b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

221_maximal_square/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test maximal_square.c

221_maximal_square/maximal_square.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
static inline int min(int a, int b)
6+
{
7+
return a < b ? a : b;
8+
}
9+
10+
static inline int max(int a, int b)
11+
{
12+
return a > b ? a : b;
13+
}
14+
15+
static int maximalSquare(char** matrix, int matrixRowSize, int matrixColSize)
16+
{
17+
int i, j, max_len = 0;
18+
int **lens = malloc(matrixRowSize * sizeof(int *));
19+
for (i = 0; i < matrixRowSize; i++) {
20+
lens[i] = malloc(matrixColSize * sizeof(int));
21+
}
22+
23+
for (i = 0; i < matrixColSize; i++) {
24+
lens[0][i] = matrix[0][i] == '1' ? 1 : 0;
25+
max_len = matrix[0][i] == '1' ? 1 : max_len;
26+
}
27+
28+
for (i = 0; i < matrixRowSize; i++) {
29+
lens[i][0] = matrix[i][0] == '1' ? 1 : 0;
30+
max_len = matrix[i][0] == '1' ? 1 : max_len;
31+
}
32+
33+
for (i = 1; i < matrixRowSize; i++) {
34+
for (j = 1; j < matrixColSize; j++) {
35+
if (matrix[i][j] == '1') {
36+
lens[i][j] = 1;
37+
lens[i][j] += min(lens[i - 1][j - 1], min(lens[i - 1][j], lens[i][j - 1]));
38+
} else {
39+
lens[i][j] = 0;
40+
}
41+
max_len = max(max_len, lens[i][j]);
42+
}
43+
}
44+
return max_len * max_len;
45+
}
46+
47+
/* ./test 11111111 11111110 11111110 11111000 01111000 */
48+
int main(int argc, char **argv)
49+
{
50+
if (argc < 2) {
51+
fprintf(stderr, "Usage: ./test row1 row2...\n");
52+
exit(-1);
53+
}
54+
55+
int i, j;
56+
int row_size = argc - 1;
57+
int col_size = strlen(argv[1]);
58+
for (i = 0; i < row_size; i++) {
59+
printf("%s\n", argv[i + 1]);
60+
}
61+
printf("%d\n", maximalSquare(argv + 1, argc - 1, strlen(argv[1])));
62+
return 0;
63+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy