[LeetCode] Flip Columns For Maximum Number of Equal Rows

1072. Flip Columns For Maximum Number of Equal Rows

You are given an m x n binary matrix matrix.

You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from 0 to 1 or vice versa).

Return the maximum number of rows that have all values equal after some number of flips.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
string hash(vector<int>& A) {
string res = "";
for(auto& a : A) res += to_string(a) + '#';
return res;
}
public:
int maxEqualRowsAfterFlips(vector<vector<int>>& A) {
for(auto& r : A) {
if(r[0] == 1)
for(auto& c : r) c = !c;
}
unordered_map<string, int> freq;
int res = 0;
for(auto& r : A) {
res = max(res, ++freq[hash(r)]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/25/PS/LeetCode/flip-columns-for-maximum-number-of-equal-rows/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.