Original Address: https://www.cnblogs.com/strengthen/p/10962559.html
Given a matrix
consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column. Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0.
Return the maximum number of rows that have all values equal after some number of flips.
Example 1:
Input: [[0,1],[1,1]]
Output: 1
Explanation: After flipping no values, 1 row has all values equal.
Example 2:
Input: [[0,1],[1,0]]
Output: 2
Explanation: After flipping values in the first column, both rows have equal values.
Example 3:
Input: [[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.
Note:
1 <= matrix.length <= 300
1 <= matrix[i].length <= 300
- All
matrix[i].length
's are equal matrix[i][j]
is0
or1
To a matrix composed of 0 and 1 by a number of predetermined matrix
, selected from any number of columns and turn each cell thereon. After inversion, the cell value from 0 to 1, or from 1 to 0.
After some maximum number of rows returned flip on line all values are equal.
Example 1:
Input: [[0,1], [1,1]] Output: 1 Explanation: no inversion, row 1 all values are equal.
Example 2:
Input: [[0,1], [1,0]] Output: 2 Explanation: after the rollover value of the first column, two rows by equal values.
Example 3:
Input: [[0,0,0], [0,0,1], [1,1,0]] Output: 2 Explanation: the value after two inverted before, the two lines of equal values.
prompt:
1 <= matrix.length <= 300
1 <= matrix[i].length <= 300
- All
matrix[i].length
are equal matrix[i][j]
Is0
or1
Runtime: 1508 ms
Memory Usage: 21.3 MB
1 class Solution { 2 func maxEqualRowsAfterFlips(_ matrix: [[Int]]) -> Int { 3 var map:[String:Int] = [String:Int]() 4 for x in matrix 5 { 6 var s:String = String() 7 let flag:Int = x[0] 8 for i in 0..<x.count 9 { 10 if x[i] == flag 11 { 12 s.append("1") 13 } 14 else 15 { 16 s.append("0") 17 } 18 } 19 map[s,default:0] += 1 20 } 21 var result:Int = 0 22 for val in map.values 23 { 24 result = max(result,val) 25 } 26 return result 27 } 28 }