-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1253. Reconstruct a 2-Row Binary Matrix.cpp
55 lines (49 loc) · 1.38 KB
/
1253. Reconstruct a 2-Row Binary Matrix.cpp
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
52
53
54
55
class Solution {
public:
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colSum) {
int twoElementInColumn = 0;
int totalSum = 0;
for(int i=0; i<colSum.size(); i++)
{
totalSum += colSum[i];
if(colSum[i] == 2){
twoElementInColumn++;
}
}
if(totalSum != (upper + lower) || (twoElementInColumn > upper) || (twoElementInColumn > lower))
{
return {};
}
vector<vector<int> > ans(2);
for(int i=0; i<colSum.size(); i++)
{
if(colSum[i] == 2)
{
ans[0].push_back(1);
ans[1].push_back(1);
upper--;
lower--;
twoElementInColumn--;
}
else if(colSum[i] == 1)
{
if(upper != 0 && upper > twoElementInColumn)
{
ans[0].push_back(1);
ans[1].push_back(0);
upper--;
}
else{
ans[1].push_back(1);
ans[0].push_back(0);
lower--;
}
}
else{
ans[0].push_back(0);
ans[1].push_back(0);
}
}
return ans;
}
};