A row or column is considered palindromic if its values read the same forward and backward.
You can flip any number of cells in grid from 0 to 1, or from 1 to
0.
Return the minimum number of cells that need to be flipped to make all rows and columns palindromic , and the total number of 1’s in griddivisible by 4.
To make all rows and columns palindromic, each group of symmetric cells (under row and column reversal) must be made equal. For each group, we can flip some cells to make all values the same, and the minimum flips for each group is the number of cells minus the majority value count. Additionally, the total number of 1’s must be divisible by 4, so we need to try all possible assignments for each group to satisfy this constraint.
classSolution {
public:int minFlips(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<pair<int,int>>> groups;
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
int a = i, b = j, c = m-1-i, d = n-1-j;
vector<pair<int,int>> group = {{a,b},{c,b},{a,d},{c,d}};
sort(group.begin(), group.end());
group.erase(unique(group.begin(), group.end()), group.end());
if (groups.empty() || groups.back() != group) groups.push_back(group);
}
}
int g = groups.size();
vector<vector<int>> dp(g+1, vector<int>(4, INT_MAX));
dp[0][0] =0;
for (int i =0; i < g; ++i) {
int cnt1 =0, sz = groups[i].size();
for (auto& p : groups[i]) cnt1 += grid[p.first][p.second];
int cnt0 = sz - cnt1;
for (int mod =0; mod <4; ++mod) {
if (dp[i][mod] == INT_MAX) continue;
// Make all 0
int new_mod = (mod +0) %4;
dp[i+1][new_mod] = min(dp[i+1][new_mod], dp[i][mod] + cnt1);
// Make all 1
new_mod = (mod + sz) %4;
dp[i+1][new_mod] = min(dp[i+1][new_mod], dp[i][mod] + cnt0);
}
}
return dp[g][0] == INT_MAX ?-1: dp[g][0];
}
};
classSolution {
funminFlips(grid: Array<IntArray>): Int {
val m = grid.size
val n = grid[0].size
val groups = mutableListOf<List<Pair<Int,Int>>>()
val vis = mutableSetOf<Pair<Int,Int>>()
for (i in0 until m) {
for (j in0 until n) {
val group = listOf(Pair(i,j), Pair(m-1-i,j), Pair(i,n-1-j), Pair(m-1-i,n-1-j)).sortedBy { it.first*100000+it.second }
val uniq = group.filter { vis.add(it) }
if (uniq.isNotEmpty()) groups.add(uniq)
}
}
val g = groups.size
val dp = Array(g+1) { IntArray(4) { Int.MAX_VALUE } }
dp[0][0] = 0for (i in0 until g) {
val cnt1 = groups[i].count { grid[it.first][it.second] ==1 }
val sz = groups[i].size
val cnt0 = sz - cnt1
for (mod in0..3) {
if (dp[i][mod] ==Int.MAX_VALUE) continuevar new_mod = (mod + 0) % 4 dp[i+1][new_mod] = minOf(dp[i+1][new_mod], dp[i][mod] + cnt1)
new_mod = (mod + sz) % 4 dp[i+1][new_mod] = minOf(dp[i+1][new_mod], dp[i][mod] + cnt0)
}
}
returnif (dp[g][0] ==Int.MAX_VALUE) -1else dp[g][0]
}
}
classSolution:
defminFlips(self, grid: list[list[int]]) -> int:
m: int = len(grid)
n: int = len(grid[0])
groups: list[list[tuple[int,int]]] = []
vis: set = set()
for i in range(m):
for j in range(n):
group = sorted({(i,j),(m-1-i,j),(i,n-1-j),(m-1-i,n-1-j)})
uniq = [p for p in group if p notin vis]
for p in uniq: vis.add(p)
if uniq:
groups.append(uniq)
g: int = len(groups)
dp: list[list[int]] = [[float('inf')]*4for _ in range(g+1)]
dp[0][0] =0for i in range(g):
cnt1: int = sum(grid[x][y] for x,y in groups[i])
sz: int = len(groups[i])
cnt0: int = sz - cnt1
for mod in range(4):
if dp[i][mod] == float('inf'): continue new_mod = (mod +0) %4 dp[i+1][new_mod] = min(dp[i+1][new_mod], dp[i][mod] + cnt1)
new_mod = (mod + sz) %4 dp[i+1][new_mod] = min(dp[i+1][new_mod], dp[i][mod] + cnt0)
return-1if dp[g][0] == float('inf') else dp[g][0]