-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflipping_matrix.cpp
82 lines (62 loc) · 2.27 KB
/
flipping_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Solution {
public:
int matrixScore(vector<vector<int>>& A) {
/* make sure all the col-0 will be 1's */
for(int i = 0 ; i < A.size() ; i++)
if(A[i][0] == 0)
for(int& element:A[i]) element=!element;
/* check col-1 from col-end */
int row_mid = (A.size()/2) + (A.size()%2);
for(int i = 1 ; i < A[0].size() ; i++)
count_1s_and_flip(A,i,row_mid);
/* sum up the binary value to res */
int res = 0;
for(int i = 0 ; i < A.size() ; i ++)
res += binary2dec(A,i);
return res;
}
/* this function is used to count if current column has more 0's than 1's, if YES, flip it */
void count_1s_and_flip(vector<vector<int>>& A, int col,int row_mid){
for(int i = 0 ; i < A.size() ;i++)
row_mid -= A[i][col];
if(row_mid > 0) // 0's is more than 1's need to flip
for(int i = 0 ; i < A.size() ;i++)
A[i][col] = !A[i][col];
}
/* this function is used to calculate row into a decimal value */
int binary2dec(vector<vector<int>>& A, int row){
int res = 0;
for(int i = A[0].size()-1,cnt = 0 ; i >= 0 ; i--,cnt++)
res += (A[row][i]<<cnt);
return res;
}
/*
//check for first column greedily..
for(int i=0; i<A.size(); i++){
for(int &element : A[i])
if(A[i][0] == 0) element = !element;
}
//check for rest of the columns..
int mid = (A.size()/2) + (A.size()%2);
for(int i=1; i<A[0].size(); i++)
count_flip(A, i, mid);
int res = 0;
for(int row = 0; row < A.size(); row++){
for(int i = A[0].size()-1,cnt = 0 ; i >= 0 ; i--,cnt++)
res += (A[row][i]<<cnt);
}
return res;
}
void count_flip(vector<vector<int>> &A, int i, int mid){
int res = 0;
for(int col=0; col<A[i].size(); col++){
mid -= A[col][i];
}
if(res > 0){
for(int col=0; col<A[i].size(); col++){
A[col][i] = !A[col][i];
}
}
}
*/
};