-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1072_maxEqualRowsAfterFlips.cc
51 lines (46 loc) · 1.14 KB
/
Problem_1072_maxEqualRowsAfterFlips.cc
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 <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include "UnitTest.h"
using namespace std;
class Solution {
public:
int maxEqualRowsAfterFlips(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
unordered_map<string, int> map;
for(int i = 0; i < m; i++)
{
string s = string(n, '0');
for(int j = 0; j < n; j++)
{
// 如果 matrix[i][j] 为 1,则对元素进行翻转
s[j] = '0' + (matrix[i][j] ^ matrix[i][0]);
}
map[s]++;
}
int ans = 0;
for(auto& [k, v]:map)
{
ans = std::max(ans, v);
}
return ans;
}
};
void testMaxEqualRowsAfterFlips()
{
Solution s;
vector<vector<int>> n1 = {{0,1},{1,1}};
vector<vector<int>> n2 = {{0,1},{1,0}};
vector<vector<int>> n3 = {{0,0,0},{0,0,1},{1,1,0}};
EXPECT_EQ_INT(1, s.maxEqualRowsAfterFlips(n1));
EXPECT_EQ_INT(2, s.maxEqualRowsAfterFlips(n2));
EXPECT_EQ_INT(2, s.maxEqualRowsAfterFlips(n3));
EXPECT_SUMMARY;
}
int main()
{
testMaxEqualRowsAfterFlips();
return 0;
}