Skip to content

Commit d31fa75

Browse files
authored
Create flip-columns-for-maximum-number-of-equal-rows.cpp
1 parent 3414a2d commit d31fa75

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(m * n)
2+
// Space: O(m * n)
3+
4+
class Solution {
5+
public:
6+
int maxEqualRowsAfterFlips(vector<vector<int>>& matrix) {
7+
unordered_map<string,int> count;
8+
for (const auto& row : matrix){
9+
++count[encode(row)];
10+
}
11+
12+
using pair_type = decltype(count)::value_type;
13+
return max_element(count.cbegin(), count.cend(),
14+
[] (const pair_type& a,
15+
const pair_type& b) {
16+
return a.second < b.second;
17+
})->second;
18+
}
19+
20+
private:
21+
string encode(const vector<int>& s) {
22+
string result;
23+
for (const auto& c : s) {
24+
result.push_back(c ^ s[0]);
25+
}
26+
return result;
27+
}
28+
};

0 commit comments

Comments
 (0)