diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md index e4359f3d48a23..67edbe2fc8b12 100644 --- a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md @@ -83,32 +83,245 @@ tags: -### 方法一 +### 方法一:分类讨论 + +行和列都是回文的,那么对于任意 $i \in [0, m / 2)$ 和 $j \in [0, n / 2)$,都需要满足 $\text{grid}[i][j] = \text{grid}[m - i - 1][j] = \text{grid}[i][n - j - 1] = \text{grid}[m - i - 1][n - j - 1]$,要么都变成 $0$,要么都变成 $1$,变成 $0$ 的次数为 $c_0 = \text{grid}[i][j] + \text{grid}[m - i - 1][j] + \text{grid}[i][n - j - 1] + \text{grid}[m - i - 1][n - j - 1]$,变成 $1$ 的次数为 $c_1 = 4 - c_0$,取两者的较小值,累加到答案中。 + +接下来,我们再讨论 $m$ 和 $n$ 的奇偶性: + +- 如果 $m$ 和 $n$ 都是偶数,那么直接返回答案; +- 如果 $m$ 和 $n$ 都是奇数,那么最中间的格子只能是 $0$,因为题目要求 $1$ 的数目可以被 $4$ 整除; +- 如果 $m$ 是奇数,而 $n$ 是偶数,那么我们需要考虑最中间的一行; +- 如果 $m$ 是偶数,而 $n$ 是奇数,那么我们需要考虑最中间的一列。 + +对于后两种情况,我们需要统计最中间的一行或一列中对应位置不相同的格子对数 $\text{diff}$,以及对应位置相同且为 $1$ 的格子个数 $\text{cnt1}$,然后再分情况讨论: + +- 如果 $\text{cnt1} \bmod 4 = 0$,那么我们只需要将 $\text{diff}$ 个格子变成 $0$ 即可,操作次数为 $\text{diff}$; +- 否则,说明 $\text{cnt1} = 2$,此时如果 $\text{diff} \lt 0$,我们可以将其中一个格子变成 $1$,使得 $\text{cnt1} = 4$,那么剩下的 $\text{diff} - 1$ 个格子变成 $0$ 即可,操作次数一共为 $\text{diff}$。 +- 否则,如果 $\text{diff} = 0$,我们就把 $\text{2}$ 个格子变成 $0$,使得 $\text{cnt1} \bmod 4 = 0$,操作次数为 $\text{2}$。 + +我们将操作次数累加到答案中,最后返回答案即可。 + +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minFlips(self, grid: List[List[int]]) -> int: + m, n = len(grid), len(grid[0]) + ans = 0 + for i in range(m // 2): + for j in range(n // 2): + x, y = m - i - 1, n - j - 1 + cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] + ans += min(cnt1, 4 - cnt1) + if m % 2 and n % 2: + ans += grid[m // 2][n // 2] + diff = cnt1 = 0 + if m % 2: + for j in range(n // 2): + if grid[m // 2][j] == grid[m // 2][n - j - 1]: + cnt1 += grid[m // 2][j] * 2 + else: + diff += 1 + if n % 2: + for i in range(m // 2): + if grid[i][n // 2] == grid[m - i - 1][n // 2]: + cnt1 += grid[i][n // 2] * 2 + else: + diff += 1 + ans += diff if cnt1 % 4 == 0 or diff else 2 + return ans ``` #### Java ```java - +class Solution { + public int minFlips(int[][] grid) { + int m = grid.length, n = grid[0].length; + int ans = 0; + for (int i = 0; i < m / 2; ++i) { + for (int j = 0; j < n / 2; ++j) { + int x = m - i - 1, y = n - j - 1; + int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += Math.min(cnt1, 4 - cnt1); + } + } + if (m % 2 == 1 && n % 2 == 1) { + ans += grid[m / 2][n / 2]; + } + + int diff = 0, cnt1 = 0; + if (m % 2 == 1) { + for (int j = 0; j < n / 2; ++j) { + if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { + cnt1 += grid[m / 2][j] * 2; + } else { + diff += 1; + } + } + } + if (n % 2 == 1) { + for (int i = 0; i < m / 2; ++i) { + if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { + cnt1 += grid[i][n / 2] * 2; + } else { + diff += 1; + } + } + } + ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minFlips(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + int ans = 0; + for (int i = 0; i < m / 2; ++i) { + for (int j = 0; j < n / 2; ++j) { + int x = m - i - 1, y = n - j - 1; + int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += min(cnt1, 4 - cnt1); + } + } + if (m % 2 == 1 && n % 2 == 1) { + ans += grid[m / 2][n / 2]; + } + + int diff = 0, cnt1 = 0; + if (m % 2 == 1) { + for (int j = 0; j < n / 2; ++j) { + if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { + cnt1 += grid[m / 2][j] * 2; + } else { + diff += 1; + } + } + } + if (n % 2 == 1) { + for (int i = 0; i < m / 2; ++i) { + if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { + cnt1 += grid[i][n / 2] * 2; + } else { + diff += 1; + } + } + } + ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; + return ans; + } +}; ``` #### Go ```go +func minFlips(grid [][]int) int { + m, n := len(grid), len(grid[0]) + ans := 0 + + for i := 0; i < m/2; i++ { + for j := 0; j < n/2; j++ { + x, y := m-i-1, n-j-1 + cnt1 := grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] + ans += min(cnt1, 4-cnt1) + } + } + + if m%2 == 1 && n%2 == 1 { + ans += grid[m/2][n/2] + } + + diff, cnt1 := 0, 0 + + if m%2 == 1 { + for j := 0; j < n/2; j++ { + if grid[m/2][j] == grid[m/2][n-j-1] { + cnt1 += grid[m/2][j] * 2 + } else { + diff += 1 + } + } + } + + if n%2 == 1 { + for i := 0; i < m/2; i++ { + if grid[i][n/2] == grid[m-i-1][n/2] { + cnt1 += grid[i][n/2] * 2 + } else { + diff += 1 + } + } + } + + if cnt1%4 == 0 || diff > 0 { + ans += diff + } else { + ans += 2 + } + + return ans +} +``` +#### TypeScript + +```ts +function minFlips(grid: number[][]): number { + const m = grid.length; + const n = grid[0].length; + let ans = 0; + + for (let i = 0; i < Math.floor(m / 2); i++) { + for (let j = 0; j < Math.floor(n / 2); j++) { + const x = m - i - 1; + const y = n - j - 1; + const cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += Math.min(cnt1, 4 - cnt1); + } + } + + if (m % 2 === 1 && n % 2 === 1) { + ans += grid[Math.floor(m / 2)][Math.floor(n / 2)]; + } + + let diff = 0, + cnt1 = 0; + + if (m % 2 === 1) { + for (let j = 0; j < Math.floor(n / 2); j++) { + if (grid[Math.floor(m / 2)][j] === grid[Math.floor(m / 2)][n - j - 1]) { + cnt1 += grid[Math.floor(m / 2)][j] * 2; + } else { + diff += 1; + } + } + } + + if (n % 2 === 1) { + for (let i = 0; i < Math.floor(m / 2); i++) { + if (grid[i][Math.floor(n / 2)] === grid[m - i - 1][Math.floor(n / 2)]) { + cnt1 += grid[i][Math.floor(n / 2)] * 2; + } else { + diff += 1; + } + } + } + + ans += cnt1 % 4 === 0 || diff > 0 ? diff : 2; + return ans; +} ``` diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md index a5ad8b4198cf3..93c7473258aa7 100644 --- a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md @@ -81,32 +81,245 @@ tags: -### Solution 1 +### Solution 1: Case Analysis + +If both rows and columns are palindromic, then for any $i \in [0, m / 2)$ and $j \in [0, n / 2)$, it must satisfy $\text{grid}[i][j] = \text{grid}[m - i - 1][j] = \text{grid}[i][n - j - 1] = \text{grid}[m - i - 1][n - j - 1]$. They must either all become $0$ or all become $1$. The number of changes to $0$ is $c_0 = \text{grid}[i][j] + \text{grid}[m - i - 1][j] + \text{grid}[i][n - j - 1] + \text{grid}[m - i - 1][n - j - 1]$, and the number of changes to $1$ is $c_1 = 4 - c_0$. We take the minimum of the two and add it to the answer. + +Next, we discuss the parity of $m$ and $n$: + +- If both $m$ and $n$ are even, we directly return the answer. +- If both $m$ and $n$ are odd, the center cell must be $0$ because the number of $1$s must be divisible by $4$. +- If $m$ is odd and $n$ is even, we need to consider the middle row. +- If $m$ is even and $n$ is odd, we need to consider the middle column. + +For the latter two cases, we need to count the number of differing pairs of cells $\text{diff}$ in the middle row or column, and the number of cells that are the same and equal to $1$ $\text{cnt1}$. Then we discuss the following cases: + +- If $\text{cnt1} \bmod 4 = 0$, we only need to change the $\text{diff}$ cells to $0$, and the number of operations is $\text{diff}$. +- Otherwise, if $\text{cnt1} = 2$, if $\text{diff} \lt 0$, we can change one of the cells to $1$ to make $\text{cnt1} = 4$, and then change the remaining $\text{diff} - 1$ cells to $0$. The total number of operations is $\text{diff}$. +- Otherwise, if $\text{diff} = 0$, we change the $2$ cells to $0$ to make $\text{cnt1} \bmod 4 = 0$, and the number of operations is $2$. + +We add the number of operations to the answer and finally return the answer. + +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def minFlips(self, grid: List[List[int]]) -> int: + m, n = len(grid), len(grid[0]) + ans = 0 + for i in range(m // 2): + for j in range(n // 2): + x, y = m - i - 1, n - j - 1 + cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] + ans += min(cnt1, 4 - cnt1) + if m % 2 and n % 2: + ans += grid[m // 2][n // 2] + diff = cnt1 = 0 + if m % 2: + for j in range(n // 2): + if grid[m // 2][j] == grid[m // 2][n - j - 1]: + cnt1 += grid[m // 2][j] * 2 + else: + diff += 1 + if n % 2: + for i in range(m // 2): + if grid[i][n // 2] == grid[m - i - 1][n // 2]: + cnt1 += grid[i][n // 2] * 2 + else: + diff += 1 + ans += diff if cnt1 % 4 == 0 or diff else 2 + return ans ``` #### Java ```java - +class Solution { + public int minFlips(int[][] grid) { + int m = grid.length, n = grid[0].length; + int ans = 0; + for (int i = 0; i < m / 2; ++i) { + for (int j = 0; j < n / 2; ++j) { + int x = m - i - 1, y = n - j - 1; + int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += Math.min(cnt1, 4 - cnt1); + } + } + if (m % 2 == 1 && n % 2 == 1) { + ans += grid[m / 2][n / 2]; + } + + int diff = 0, cnt1 = 0; + if (m % 2 == 1) { + for (int j = 0; j < n / 2; ++j) { + if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { + cnt1 += grid[m / 2][j] * 2; + } else { + diff += 1; + } + } + } + if (n % 2 == 1) { + for (int i = 0; i < m / 2; ++i) { + if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { + cnt1 += grid[i][n / 2] * 2; + } else { + diff += 1; + } + } + } + ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minFlips(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + int ans = 0; + for (int i = 0; i < m / 2; ++i) { + for (int j = 0; j < n / 2; ++j) { + int x = m - i - 1, y = n - j - 1; + int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += min(cnt1, 4 - cnt1); + } + } + if (m % 2 == 1 && n % 2 == 1) { + ans += grid[m / 2][n / 2]; + } + + int diff = 0, cnt1 = 0; + if (m % 2 == 1) { + for (int j = 0; j < n / 2; ++j) { + if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { + cnt1 += grid[m / 2][j] * 2; + } else { + diff += 1; + } + } + } + if (n % 2 == 1) { + for (int i = 0; i < m / 2; ++i) { + if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { + cnt1 += grid[i][n / 2] * 2; + } else { + diff += 1; + } + } + } + ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; + return ans; + } +}; ``` #### Go ```go +func minFlips(grid [][]int) int { + m, n := len(grid), len(grid[0]) + ans := 0 + + for i := 0; i < m/2; i++ { + for j := 0; j < n/2; j++ { + x, y := m-i-1, n-j-1 + cnt1 := grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] + ans += min(cnt1, 4-cnt1) + } + } + + if m%2 == 1 && n%2 == 1 { + ans += grid[m/2][n/2] + } + + diff, cnt1 := 0, 0 + + if m%2 == 1 { + for j := 0; j < n/2; j++ { + if grid[m/2][j] == grid[m/2][n-j-1] { + cnt1 += grid[m/2][j] * 2 + } else { + diff += 1 + } + } + } + + if n%2 == 1 { + for i := 0; i < m/2; i++ { + if grid[i][n/2] == grid[m-i-1][n/2] { + cnt1 += grid[i][n/2] * 2 + } else { + diff += 1 + } + } + } + + if cnt1%4 == 0 || diff > 0 { + ans += diff + } else { + ans += 2 + } + + return ans +} +``` +#### TypeScript + +```ts +function minFlips(grid: number[][]): number { + const m = grid.length; + const n = grid[0].length; + let ans = 0; + + for (let i = 0; i < Math.floor(m / 2); i++) { + for (let j = 0; j < Math.floor(n / 2); j++) { + const x = m - i - 1; + const y = n - j - 1; + const cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += Math.min(cnt1, 4 - cnt1); + } + } + + if (m % 2 === 1 && n % 2 === 1) { + ans += grid[Math.floor(m / 2)][Math.floor(n / 2)]; + } + + let diff = 0, + cnt1 = 0; + + if (m % 2 === 1) { + for (let j = 0; j < Math.floor(n / 2); j++) { + if (grid[Math.floor(m / 2)][j] === grid[Math.floor(m / 2)][n - j - 1]) { + cnt1 += grid[Math.floor(m / 2)][j] * 2; + } else { + diff += 1; + } + } + } + + if (n % 2 === 1) { + for (let i = 0; i < Math.floor(m / 2); i++) { + if (grid[i][Math.floor(n / 2)] === grid[m - i - 1][Math.floor(n / 2)]) { + cnt1 += grid[i][Math.floor(n / 2)] * 2; + } else { + diff += 1; + } + } + } + + ans += cnt1 % 4 === 0 || diff > 0 ? diff : 2; + return ans; +} ``` diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.cpp b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.cpp new file mode 100644 index 0000000000000..1bf09348fd670 --- /dev/null +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int minFlips(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + int ans = 0; + for (int i = 0; i < m / 2; ++i) { + for (int j = 0; j < n / 2; ++j) { + int x = m - i - 1, y = n - j - 1; + int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += min(cnt1, 4 - cnt1); + } + } + if (m % 2 == 1 && n % 2 == 1) { + ans += grid[m / 2][n / 2]; + } + + int diff = 0, cnt1 = 0; + if (m % 2 == 1) { + for (int j = 0; j < n / 2; ++j) { + if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { + cnt1 += grid[m / 2][j] * 2; + } else { + diff += 1; + } + } + } + if (n % 2 == 1) { + for (int i = 0; i < m / 2; ++i) { + if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { + cnt1 += grid[i][n / 2] * 2; + } else { + diff += 1; + } + } + } + ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; + return ans; + } +}; diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.go b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.go new file mode 100644 index 0000000000000..8fd226b508d96 --- /dev/null +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.go @@ -0,0 +1,46 @@ +func minFlips(grid [][]int) int { + m, n := len(grid), len(grid[0]) + ans := 0 + + for i := 0; i < m/2; i++ { + for j := 0; j < n/2; j++ { + x, y := m-i-1, n-j-1 + cnt1 := grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] + ans += min(cnt1, 4-cnt1) + } + } + + if m%2 == 1 && n%2 == 1 { + ans += grid[m/2][n/2] + } + + diff, cnt1 := 0, 0 + + if m%2 == 1 { + for j := 0; j < n/2; j++ { + if grid[m/2][j] == grid[m/2][n-j-1] { + cnt1 += grid[m/2][j] * 2 + } else { + diff += 1 + } + } + } + + if n%2 == 1 { + for i := 0; i < m/2; i++ { + if grid[i][n/2] == grid[m-i-1][n/2] { + cnt1 += grid[i][n/2] * 2 + } else { + diff += 1 + } + } + } + + if cnt1%4 == 0 || diff > 0 { + ans += diff + } else { + ans += 2 + } + + return ans +} diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.java b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.java new file mode 100644 index 0000000000000..9ff80eee60cd2 --- /dev/null +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.java @@ -0,0 +1,38 @@ +class Solution { + public int minFlips(int[][] grid) { + int m = grid.length, n = grid[0].length; + int ans = 0; + for (int i = 0; i < m / 2; ++i) { + for (int j = 0; j < n / 2; ++j) { + int x = m - i - 1, y = n - j - 1; + int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += Math.min(cnt1, 4 - cnt1); + } + } + if (m % 2 == 1 && n % 2 == 1) { + ans += grid[m / 2][n / 2]; + } + + int diff = 0, cnt1 = 0; + if (m % 2 == 1) { + for (int j = 0; j < n / 2; ++j) { + if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { + cnt1 += grid[m / 2][j] * 2; + } else { + diff += 1; + } + } + } + if (n % 2 == 1) { + for (int i = 0; i < m / 2; ++i) { + if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { + cnt1 += grid[i][n / 2] * 2; + } else { + diff += 1; + } + } + } + ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; + return ans; + } +} diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.py b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.py new file mode 100644 index 0000000000000..1c0d766d7f1e3 --- /dev/null +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.py @@ -0,0 +1,26 @@ +class Solution: + def minFlips(self, grid: List[List[int]]) -> int: + m, n = len(grid), len(grid[0]) + ans = 0 + for i in range(m // 2): + for j in range(n // 2): + x, y = m - i - 1, n - j - 1 + cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] + ans += min(cnt1, 4 - cnt1) + if m % 2 and n % 2: + ans += grid[m // 2][n // 2] + diff = cnt1 = 0 + if m % 2: + for j in range(n // 2): + if grid[m // 2][j] == grid[m // 2][n - j - 1]: + cnt1 += grid[m // 2][j] * 2 + else: + diff += 1 + if n % 2: + for i in range(m // 2): + if grid[i][n // 2] == grid[m - i - 1][n // 2]: + cnt1 += grid[i][n // 2] * 2 + else: + diff += 1 + ans += diff if cnt1 % 4 == 0 or diff else 2 + return ans diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.ts b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.ts new file mode 100644 index 0000000000000..31d8d1ea0170f --- /dev/null +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/Solution.ts @@ -0,0 +1,44 @@ +function minFlips(grid: number[][]): number { + const m = grid.length; + const n = grid[0].length; + let ans = 0; + + for (let i = 0; i < Math.floor(m / 2); i++) { + for (let j = 0; j < Math.floor(n / 2); j++) { + const x = m - i - 1; + const y = n - j - 1; + const cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; + ans += Math.min(cnt1, 4 - cnt1); + } + } + + if (m % 2 === 1 && n % 2 === 1) { + ans += grid[Math.floor(m / 2)][Math.floor(n / 2)]; + } + + let diff = 0, + cnt1 = 0; + + if (m % 2 === 1) { + for (let j = 0; j < Math.floor(n / 2); j++) { + if (grid[Math.floor(m / 2)][j] === grid[Math.floor(m / 2)][n - j - 1]) { + cnt1 += grid[Math.floor(m / 2)][j] * 2; + } else { + diff += 1; + } + } + } + + if (n % 2 === 1) { + for (let i = 0; i < Math.floor(m / 2); i++) { + if (grid[i][Math.floor(n / 2)] === grid[m - i - 1][Math.floor(n / 2)]) { + cnt1 += grid[i][Math.floor(n / 2)] * 2; + } else { + diff += 1; + } + } + } + + ans += cnt1 % 4 === 0 || diff > 0 ? diff : 2; + return ans; +}