generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.c
51 lines (44 loc) · 1.04 KB
/
solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
int** reconstructMatrix(
int upper, int lower, int* colsum, int colsumSize,
int* returnSize, int** returnColumnSizes) {
int** mat = malloc(2 * sizeof(int*));
*returnColumnSizes = malloc(2 * sizeof(int));
*returnSize = 2;
mat[0] = calloc(colsumSize, sizeof(int));
(*returnColumnSizes)[0] = colsumSize;
mat[1] = calloc(colsumSize, sizeof(int));
(*returnColumnSizes)[1] = colsumSize;
for (int i = colsumSize - 1; i >= 0; --i) {
if (colsum[i] == 2) {
if (lower > 0 && upper > 0) {
mat[1][i] = 1;
mat[0][i] = 1;
--lower;
--upper;
} else {
*returnSize = 0;
return mat;
}
}
}
for (int i = colsumSize - 1; i >= 0; --i) {
if (colsum[i] == 1) {
if (lower > 0) {
mat[1][i] = 1;
--lower;
} else if (upper > 0) {
mat[0][i] = 1;
--upper;
} else {
*returnSize = 0;
return mat;
}
}
}
if (upper > 0 || lower > 0) {
*returnSize = 0;
return mat;
}
return mat;
}