leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0861.cpp (596B)
0 class Solution { 1 public: 2 int matrixScore(const vector<vector<int>> &grid) { 3 int n = grid.size(), m = grid[0].size(); 4 unordered_set<int> flipped; 5 for (int i = 0; i < n; i++) { 6 if (grid[i][0]) continue; 7 flipped.insert(i); 8 } 9 10 int res = n * (1 << m - 1); 11 for (int j = 1; j < m; j++) { 12 int count = 0; 13 for (int i = 0; i < n; i++) 14 count += flipped.count(i) ? !grid[i][j] : grid[i][j]; 15 res += max(count, n - count) * (1 << m - j - 1); 16 } 17 return res; 18 } 19 };