0861.cpp (596B)
1 class Solution { 2 public: 3 int matrixScore(const vector<vector<int>> &grid) { 4 int n = grid.size(), m = grid[0].size(); 5 unordered_set<int> flipped; 6 for (int i = 0; i < n; i++) { 7 if (grid[i][0]) continue; 8 flipped.insert(i); 9 } 10 11 int res = n * (1 << m - 1); 12 for (int j = 1; j < m; j++) { 13 int count = 0; 14 for (int i = 0; i < n; i++) 15 count += flipped.count(i) ? !grid[i][j] : grid[i][j]; 16 res += max(count, n - count) * (1 << m - j - 1); 17 } 18 return res; 19 } 20 };