-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1072.java
More file actions
67 lines (66 loc) · 2 KB
/
Copy path1072.java
File metadata and controls
67 lines (66 loc) · 2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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.
//
//
//
// Example 1:
//
// Input: matrix = [[0,1],[1,1]]
// Output: 1
// Explanation: After flipping no values, 1 row has all values equal.
// Example 2:
//
// Input: matrix = [[0,1],[1,0]]
// Output: 2
// Explanation: After flipping values in the first column, both rows have equal values.
// Example 3:
//
// Input: matrix = [[0,0,0],[0,0,1],[1,1,0]]
// Output: 2
// Explanation: After flipping values in the first two columns, the last two rows have equal values.
//
//
// Constraints:
//
// m == matrix.length
// n == matrix[i].length
// 1 <= m, n <= 300
// matrix[i][j] is either 0 or 1.
//
// Runtime 24 ms Beats 76.60% of users with Java
// Memory 58.50 MB Beats 23.40% of users with Java
class Solution {
public int maxEqualRowsAfterFlips(int[][] matrix) {
int maxCount = 0;
Map<String, Integer> patterns = new HashMap<>();
for (int i = 0; i < matrix.length; i++) {
StringBuilder sb = new StringBuilder();
if (matrix[i][0] == 0) {
for (int j = 0; j < matrix[0].length; j++) {
sb.append(matrix[i][j]);
}
} else {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) sb.append(1);
else sb.append(0);
}
}
String key = sb.toString();
int count = patterns.getOrDefault(key, 0) + 1;
patterns.put(key, count);
maxCount = Math.max(count, maxCount);
}
return maxCount;
}
}
/*
target: count 00000 or 11111
choose 0000 as target
make the rows with prefix 1 to be prefix 0
-> count the number of same rows
*/