-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay74.cpp
42 lines (40 loc) · 1.06 KB
/
Day74.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
public:
int count_swaps(vector<vector<char>> matrix, char ch) {
int n = matrix.size();
int m = matrix[0].size();
int swaps = 0;
for(int i = 0; i < n; i++) {
ch = (ch == 'B') ? 'A' : 'B';
for(int j = 0; j < m; j++) {
if(j % 2 == 0 && matrix[i][j] != ch) {
swaps++;
}
else if(j % 2 == 1 && matrix[i][j] == ch) {
swaps++;
}
}
}
return swaps/2;
}
int MinimumExchange(vector<vector<char>>matrix){
int ans = INT_MAX;
int cnt_b = 0;
int n = matrix.size();
int m = matrix[0].size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(matrix[i][j] == 'B') cnt_b++;
}
}
if(n % 2 == 1 && m % 2 == 1) {
if(cnt_b > (n*m) / 2) {
ans = min(ans, count_swaps(matrix, 'A'));
}
else {
ans = min(ans, count_swaps(matrix, 'B'));
}
}
else {
ans = min(ans, min(count_swaps(matrix, 'A'), count_swaps(matrix, 'B')));
}
return ans;