给定一个具有N行和M列的矩阵,任务是找到使给定矩阵的所有行和列成为回文所需的最小替换。
例子:
Input: a[][] = {{1, 2, 3}, {4, 5, 3}, {1, 2, 1}}
Output: 2
Explanation: To make the given matrix palindromic, replace a[0][2] by 1 and replace a[1][2] by 4.
Input: a[][] = {{1, 2, 4}, {4, 5, 6}}
Output: 3
Explanation: To make a given matrix palindromic, replace a[0][0] by 4, a[1][2] by 4 and a[[0][1] by 5.
方法:我们的想法是选择矩阵的四个单元格的集合,如下所述并相应地进行。
- 要使矩阵的每一行和每一列都是回文,遍历给定的矩阵,对于每个单元格(i, j) (i = [0, N/2], j = [0, M/2]),选择一组以下四个单元格:
- 使所有这四个单元格的值等于其中四个单元格中出现频率最高的值。计算因此需要的替换。
- 完成上述步骤后,打印执行的替换计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count minimum
// changes to make the matrix
// palindromic
int minchanges(vector > mat)
{
// Rows in the matrix
int N = mat.size();
// Columns in the matrix
int M = mat[0].size();
int i, j, ans = 0, x;
map mp;
// Traverse the given matrix
for (i = 0; i < N / 2; i++) {
for (j = 0; j < M / 2; j++) {
// Store the frequency of
// the four cells
mp[mat[i][M - 1 - j]]++;
mp[mat[i][j]]++;
mp[mat[N - 1 - i][M - 1 - j]]++;
mp[mat[N - 1 - i][j]]++;
x = 0;
// Iterate over the map
for (auto it = mp.begin();
it != mp.end(); it++) {
x = max(x, it->second);
}
// Min changes to do to make all
ans = ans + 4 - x;
// Four elements equal
mp.clear();
}
}
// Make the middle row palindromic
if (N % 2 == 1) {
for (i = 0; i < M / 2; i++) {
if (mat[N / 2][i]
!= mat[N / 2][M - 1 - i])
ans++;
}
}
if (M % 2 == 1) {
for (i = 0; i < N / 2; i++) {
// Make the middle column
// palindromic
if (mat[i][M / 2]
!= mat[N - 1 - i][M / 2])
ans++;
}
}
// Print minimum changes
cout << ans;
}
// Driver Code
int main()
{
// Given matrix
vector > mat{ { 1, 2, 3 },
{ 4, 5, 3 },
{ 1, 2, 1 } };
// Function Call
minchanges(mat);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count minimum
// changes to make the matrix
// palindromic
static void minchanges(int [][]mat)
{
// Rows in the matrix
int N = mat.length;
// Columns in the matrix
int M = mat[0].length;
int i, j, ans = 0, x;
HashMap mp = new HashMap();
// Traverse the given matrix
for (i = 0; i < N / 2; i++)
{
for (j = 0; j < M / 2; j++)
{
// Store the frequency of
// the four cells
if(mp.containsKey(mat[i][M - 1 - j]))
{
mp.put(mat[i][M - 1 - j],
mp.get(mat[i][M - 1 - j]) + 1);
}
else
{
mp.put(mat[i][M - 1 - j], 1);
}
if(mp.containsKey(mat[i][j]))
{
mp.put(mat[i][j], mp.get(mat[i][j]) + 1);
}
else
{
mp.put(mat[i][j], 1);
}
if(mp.containsKey(mat[N - 1 - i][M - 1 - j]))
{
mp.put(mat[N - 1 - i][M - 1 - j],
mp.get(mat[N - 1 - i][M - 1 - j]) + 1);
}
else
{
mp.put(mat[N - 1 - i][M - 1 - j], 1);
}
if(mp.containsKey(mat[N - 1 - i][j]))
{
mp.put(mat[N - 1 - i][j],
mp.get(mat[N - 1 - i][j])+1);
}
else
{
mp.put(mat[N - 1 - i][j], 1);
}
x = 0;
// Iterate over the map
for (Map.Entry it : mp.entrySet())
{
x = Math.max(x, it.getValue());
}
// Min changes to do to make all
ans = ans + 4 - x;
// Four elements equal
mp.clear();
}
}
// Make the middle row palindromic
if (N % 2 == 1)
{
for (i = 0; i < M / 2; i++)
{
if (mat[N / 2][i]
!= mat[N / 2][M - 1 - i])
ans++;
}
}
if (M % 2 == 1)
{
for (i = 0; i < N / 2; i++)
{
// Make the middle column
// palindromic
if (mat[i][M / 2]
!= mat[N - 1 - i][M / 2])
ans++;
}
}
// Print minimum changes
System.out.print(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given matrix
int [][]mat = { { 1, 2, 3 },
{ 4, 5, 3 },
{ 1, 2, 1 } };
// Function Call
minchanges(mat);
}
}
// This code is contributed by shikhasingrajput
Python
# Python program for the above approach
# Function to count minimum
# changes to make the matrix
# palindromic
def minchanges(mat) :
# Rows in the matrix
N = len(mat)
# Columns in the matrix
M = len(mat[0])
ans = 0
mp = {}
# Traverse the given matrix
for i in range(N//2):
for j in range(M//2):
# Store the frequency of
# the four cells
mp[mat[i][M - 1 - j]] = mp.get(mat[i][M - 1 - j], 0) + 1
mp[mat[i][j]] = mp.get(mat[i][j], 0) + 1
mp[mat[N - 1 - i][M - 1 - j]] = mp.get(mat[N - 1 - i][M - 1 - j], 0) + 1
mp[mat[N - 1 - i][j]] = mp.get(mat[N - 1 - i][j], 0) + 1
x = 0
# Iterate over the map
for it in mp:
x = max(x, mp[it])
# Min changes to do to make all
ans = ans + 4 - x
# Four elements equal
mp.clear()
# Make the middle row palindromic
if (N % 2 == 1) :
for i in range(M // 2):
if (mat[N // 2][i]
!= mat[N // 2][M - 1 - i]) :
ans += 1
if (M % 2 == 1) :
for i in range(N // 2):
# Make the middle column
# palindromic
if (mat[i][M // 2]
!= mat[N - 1 - i][M // 2]):
ans += 1
# Prminimum changes
print(ans)
# Driver Code
# Given matrix
mat = [[ 1, 2, 3 ],
[ 4, 5, 3 ],
[1, 2, 1 ]]
# Function Call
minchanges(mat)
# This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count minimum
// changes to make the matrix
// palindromic
static void minchanges(int [,]mat)
{
// Rows in the matrix
int N = mat.GetLength(0);
// Columns in the matrix
int M = mat.GetLength(1);
int i, j, ans = 0, x;
Dictionary mp = new Dictionary();
// Traverse the given matrix
for (i = 0; i < N / 2; i++)
{
for (j = 0; j < M / 2; j++)
{
// Store the frequency of
// the four cells
if(mp.ContainsKey(mat[i,M - 1 - j]))
{
mp[mat[i,M - 1 - j]]=
mp[mat[i,M - 1 - j]] + 1;
}
else
{
mp.Add(mat[i,M - 1 - j], 1);
}
if(mp.ContainsKey(mat[i,j]))
{
mp[mat[i,j]] = mp[mat[i,j]] + 1;
}
else
{
mp.Add(mat[i,j], 1);
}
if(mp.ContainsKey(mat[N - 1 - i,M - 1 - j]))
{
mp[mat[N - 1 - i,M - 1 - j]]=
mp[mat[N - 1 - i,M - 1 - j]] + 1;
}
else
{
mp.Add(mat[N - 1 - i,M - 1 - j], 1);
}
if(mp.ContainsKey(mat[N - 1 - i,j]))
{
mp[mat[N - 1 - i,j]] =
mp[mat[N - 1 - i,j]]+1;
}
else
{
mp.Add(mat[N - 1 - i,j], 1);
}
x = 0;
// Iterate over the map
foreach (KeyValuePair it in mp)
{
x = Math.Max(x, it.Value);
}
// Min changes to do to make all
ans = ans + 4 - x;
// Four elements equal
mp.Clear();
}
}
// Make the middle row palindromic
if (N % 2 == 1)
{
for (i = 0; i < M / 2; i++)
{
if (mat[N / 2,i]
!= mat[N / 2,M - 1 - i])
ans++;
}
}
if (M % 2 == 1)
{
for (i = 0; i < N / 2; i++)
{
// Make the middle column
// palindromic
if (mat[i,M / 2]
!= mat[N - 1 - i,M / 2])
ans++;
}
}
// Print minimum changes
Console.Write(ans);
}
// Driver Code
public static void Main(String[] args)
{
// Given matrix
int [,]mat = { { 1, 2, 3 },
{ 4, 5, 3 },
{ 1, 2, 1 } };
// Function Call
minchanges(mat);
}
}
// This code contributed by shikhasingrajput
Javascript
输出:
2
时间复杂度: O(N × M)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。