Skip to content

Commit 9c7d1b5

Browse files
committed
[LeetCode Sync] Runtime - 15 ms (85.21%), Memory - 69.8 MB (87.64%)
1 parent 8bf6615 commit 9c7d1b5

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>You are given an <code>m x n</code> binary matrix <code>matrix</code>.</p>
2+
3+
<p>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 <code>0</code> to <code>1</code> or vice versa).</p>
4+
5+
<p>Return <em>the maximum number of rows that have all values equal after some number of flips</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> matrix = [[0,1],[1,1]]
12+
<strong>Output:</strong> 1
13+
<strong>Explanation:</strong> After flipping no values, 1 row has all values equal.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> matrix = [[0,1],[1,0]]
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong> After flipping values in the first column, both rows have equal values.
22+
</pre>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> matrix = [[0,0,0],[0,0,1],[1,1,0]]
28+
<strong>Output:</strong> 2
29+
<strong>Explanation:</strong> After flipping values in the first two columns, the last two rows have equal values.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>m == matrix.length</code></li>
37+
<li><code>n == matrix[i].length</code></li>
38+
<li><code>1 &lt;= m, n &lt;= 300</code></li>
39+
<li><code>matrix[i][j]</code> is either&nbsp;<code>0</code> or <code>1</code>.</li>
40+
</ul>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int maxEqualRowsAfterFlips(vector<vector<int>>& matrix) {
4+
unordered_map<string, int> map;
5+
6+
for (auto& r : matrix) {
7+
// generate the pattern for the current row
8+
string s(r.size(), 'T');
9+
for (int i = 1; i < r.size(); i++) {
10+
if (r[i] != r[0]) s[i] = 'F';
11+
}
12+
// put the pattern to map
13+
map[s]++;
14+
}
15+
16+
// find the highest freq of the pattern
17+
int ans = 0;
18+
for (auto& p : map)
19+
ans = max(ans, p.second);
20+
21+
return ans;
22+
}
23+
};

0 commit comments

Comments
 (0)