100387. 最少翻转次数使二进制矩阵回文 I
给你一个 m x n
的二进制矩阵 grid
。
如果矩阵中一行或者一列从前往后与从后往前读是一样的,那么我们称这一行或者这一列是 回文 的。
你可以将 grid
中任意格子的值 翻转 ,也就是将格子里的值从 0
变成 1
,或者从 1
变成 0
。
请你返回 最少 翻转次数,使得矩阵 要么 所有行是 回文的 ,要么所有列是 回文的 。
示例 1:
输入:grid = [[1,0,0],[0,0,0],[0,0,1]]
输出:2
解释:
将高亮的格子翻转,得到所有行都是回文的。
示例 2:
输入:grid = [[0,1],[0,1],[0,0]]
输出:1
解释:
将高亮的格子翻转,得到所有列都是回文的。
示例 3:
输入:grid = [[1],[0]]
输出:0
解释:
所有行已经是回文的。
提示:
m == grid.length
n == grid[i].length
1 <= m * n <= 2 * 105
0 <= grid[i][j] <= 1
相似题目
原站题解
rust 解法, 执行用时: 3 ms, 内存消耗: 14.8 MB, 提交时间: 2024-11-15 09:26:58
impl Solution { pub fn min_flips(grid: Vec<Vec<i32>>) -> i32 { let m = grid.len(); let n = grid[0].len(); let mut diff_row = 0; for row in &grid { for j in 0..n / 2 { if row[j] != row[n - 1 - j] { diff_row += 1; } } } let mut diff_col = 0; for j in 0..n { for i in 0..m / 2 { if grid[i][j] != grid[m - 1 - i][j] { diff_col += 1; } } } diff_row.min(diff_col) } }
javascript 解法, 执行用时: 12 ms, 内存消耗: 86.7 MB, 提交时间: 2024-11-15 09:26:39
/** * @param {number[][]} grid * @return {number} */ var minFlips = function(grid) { const m = grid.length, n = grid[0].length; let diffRow = 0; for (const row of grid) { for (let j = 0; j < n / 2; j++) { if (row[j] !== row[n - 1 - j]) { diffRow++; } } } let diffCol = 0; for (let j = 0; j < n; j++) { for (let i = 0; i < m / 2; i++) { if (grid[i][j] !== grid[m - 1 - i][j]) { diffCol++; } } } return Math.min(diffRow, diffCol); };
golang 解法, 执行用时: 263 ms, 内存消耗: 21.6 MB, 提交时间: 2024-08-08 09:34:35
func minFlips(grid [][]int) int { m, n := len(grid), len(grid[0]) diffRow := 0 for _, row := range grid { for j := 0; j < n/2; j++ { if row[j] != row[n-1-j] { diffRow++ } } } diffCol := 0 for j := 0; j < n; j++ { for i, row := range grid[:m/2] { if row[j] != grid[m-1-i][j] { diffCol++ } } } return min(diffRow, diffCol) }
cpp 解法, 执行用时: 320 ms, 内存消耗: 177.3 MB, 提交时间: 2024-08-08 09:34:10
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); } };
java 解法, 执行用时: 9 ms, 内存消耗: 108.3 MB, 提交时间: 2024-08-08 09:33:49
class Solution { public int minFlips(int[][] grid) { int m = grid.length; int n = grid[0].length; int diffRow = 0; for (int[] row : grid) { for (int j = 0; j < n / 2; j++) { if (row[j] != row[n - 1 - j]) { diffRow++; } } } int diffCol = 0; for (int j = 0; j < n; j++) { for (int i = 0; i < m / 2; i++) { if (grid[i][j] != grid[m - 1 - i][j]) { diffCol++; } } } return Math.min(diffRow, diffCol); } }
python3 解法, 执行用时: 322 ms, 内存消耗: 55.9 MB, 提交时间: 2024-08-08 09:32:49
# 分别计算全部行/列回文的翻转次数和,取小的 class Solution: def minFlips(self, grid: List[List[int]]) -> int: diff_row = 0 for row in grid: for j in range(len(row) // 2): if row[j] != row[-1 - j]: diff_row += 1 diff_col = 0 for col in zip(*grid): for i in range(len(grid) // 2): if col[i] != col[-1 - i]: diff_col += 1 return min(diff_row, diff_col)