-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1253. Reconstruct a 2-Row Binary Matrix.cpp
More file actions
68 lines (62 loc) · 2.17 KB
/
Copy path1253. Reconstruct a 2-Row Binary Matrix.cpp
File metadata and controls
68 lines (62 loc) · 2.17 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
//https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/
class Solution {
int getUpperSum(const vector<vector<int>>& data) {
int upperSum = 0;
for(int j= 0; j < data[0].size(); j++) {
upperSum += data[0][j];
}
return upperSum;
}
int getLowerSum(const vector<vector<int>>& data) {
int lowerSum = 0;
for(int j= 0; j < data[1].size(); j++) {
lowerSum += data[1][j];
}
return lowerSum;
}
public:
/**
Since we have only two rows.
So wherever colsum is 2 fill those both cells on as 1.
Check if upper and lower is bigger then expected then result is not possible.
Else fill with greedy approach for the cell containing 0.
**/
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {
vector<vector<int>> result;
vector<vector<int>> data(2);
for(int i = 0 ; i < data.size(); i++) {
data[i].resize(colsum.size(),0);
}
// Fill each column which has sum two.
for(int j= 0 ; j < colsum.size(); j++) {
if ( colsum[j] == 2) {
data[0][j] = 1;
data[1][j] = 1;
}
}
int upperSum = getUpperSum(data) - upper;
int lowerSum = getLowerSum(data) - lower;
// Still result might be possible. Try greedy apporach to fill all colSum 1.
if ( upperSum <= 0 && lowerSum <= 0) {
bool resultFound = true;
for(int j= 0 ; j < colsum.size(); j++) {
if ( colsum[j] == 1) {
if ( upperSum < 0) {
data[0][j] = 1;
upperSum += 1;
} else if (lowerSum < 0) {
data[1][j] = 1;
lowerSum += 1;
} else {
resultFound = false;
break;
}
}
}
if ( resultFound && getUpperSum(data) == upper && getLowerSum(data) == lower) {
result = data;
}
}
return result;
}
};