forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
f2c3c3a
commit afc05f0
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test maximal_rectangle.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
int maximalRectangle(char** matrix, int matrixRowSize, int matrixColSize) { | ||
int i, j, max_area = 0; | ||
for (i = 0; i < matrixRowSize; i++) { | ||
for (j = 0; j < matrixColSize; j++) { | ||
if (matrix[i][j] == '1') { | ||
int area = 0, x, y; | ||
int row = i; | ||
int min_col = matrixColSize; | ||
while (row < matrixRowSize) { | ||
for (x = j; x < matrixColSize && matrix[row][x] == '1'; x++) {} | ||
min_col = x < min_col ? x : min_col; | ||
area = (row - i + 1) * (min_col - j); | ||
max_area = area > max_area ? area : max_area; | ||
row++; | ||
} | ||
int col = j; | ||
int min_row = matrixRowSize; | ||
while (col < matrixColSize) { | ||
for (y = i; y < matrixRowSize && matrix[y][col] == '1'; y++) {} | ||
min_row = y < min_row ? y : min_row; | ||
area = (min_row - i) * (col - j + 1); | ||
max_area = area > max_area ? area : max_area; | ||
col++; | ||
} | ||
} | ||
} | ||
} | ||
return max_area; | ||
} | ||
|
||
/* ./test 11111111 11111110 11111110 11111000 01111000 */ | ||
int main(int argc, char **argv) | ||
{ | ||
if (argc < 2) { | ||
fprintf(stderr, "Usage: ./test row1 row2...\n"); | ||
exit(-1); | ||
} | ||
|
||
int i, j; | ||
int row_size = argc - 1; | ||
int col_size = strlen(argv[1]); | ||
for (i = 0; i < row_size; i++) { | ||
printf("%s\n", argv[i + 1]); | ||
} | ||
printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1]))); | ||
return 0; | ||
} |