Skip to content

Commit 5926a5d

Browse files
authored
Create 1072. Flip Columns For Maximum Number of Equal Rows (#640)
2 parents 9845cfd + 05311ef commit 5926a5d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int maxEqualRowsAfterFlips(vector<vector<int>>& matrix) {
4+
unordered_map<string, int> patternCount; // To store the pattern and its count
5+
6+
for (const auto& row : matrix) {
7+
string pattern = "";
8+
9+
// Generate a pattern string for this row based on the first row
10+
for (int j = 0; j < row.size(); ++j) {
11+
// Compare with the first element in the row to determine if flip is needed
12+
pattern += (row[j] ^ row[0]) ? '1' : '0';
13+
}
14+
15+
// Increment the count for this pattern
16+
patternCount[pattern]++;
17+
}
18+
19+
// Find the maximum count of rows with the same pattern
20+
int maxRows = 0;
21+
for (const auto& entry : patternCount) {
22+
maxRows = max(maxRows, entry.second);
23+
}
24+
25+
return maxRows;
26+
}
27+
};

0 commit comments

Comments
 (0)