3239. 最少翻转次数使二进制矩阵回文 I
题目链接:3239. 最少翻转次数使二进制矩阵回文 I
代码如下:
class Solution
{
public:
int minFlips(vector<vector<int>>& grid)
{
int m = grid.size(), n = grid[0].size();
int diff_row = 0;
for (auto& row : grid)
{
for (int j = 0; j < n / 2; j++)
{
diff_row += row[j] != row[n - 1 - j];
}
}
int diff_col = 0;
for (int j = 0; j < n; j++)
{
for (int i = 0; i < m / 2; i++)
{
diff_col += grid[i][j] != grid[m - 1 - i][j];
}
}
return min(diff_row, diff_col);
}
};