classSolution { private: intforLine(vector<vector<int>>& grid){ int ans = 0; int n = grid.size(), m = grid[0].size(); for (int i = 0; i < n; i++) { for (int j = 0; j < m / 2; j++) { ans += grid[i][j] != grid[i][m - j - 1]; } } return ans; }
intforCol(vector<vector<int>>& grid){ int ans = 0; int n = grid.size(), m = grid[0].size(); for (int j = 0; j < m; j++) { for (int i = 0; i < n / 2; i++) { ans += grid[i][j] != grid[n - i - 1][j]; } } return ans; } public: intminFlips(vector<vector<int>>& grid){ returnmin(forLine(grid), forCol(grid)); } };
classSolution: defforLine(self, grid: List[List[int]]) -> int: ans = 0 n, m = len(grid), len(grid[0]) for i inrange(n): for j inrange(m // 2): ans += grid[i][j] != grid[i][m - j - 1] return ans
defforCol(self, grid: List[List[int]]) -> int: ans = 0 n, m = len(grid), len(grid[0]) for j inrange(m): for i inrange(n // 2): ans += grid[i][j] != grid[n - i - 1][j] return ans
funcforLine(grid [][]int) (ans int) { n, m := len(grid), len(grid[0]) for i := 0; i < n; i++ { for j := 0; j < m / 2; j++ { if grid[i][j] != grid[i][m - j - 1] { ans++ } } } return }
funcforCol(grid [][]int) (ans int) { n, m := len(grid), len(grid[0]) for j := 0; j < m; j++ { for i := 0; i < n / 2; i++ { if grid[i][j] != grid[n - i - 1][j] { ans++ } } } return }