给定尺寸为N * M的矩阵mat [] [] ,任务是计算所需的不同数组元素的最小递减数量,以使两个相邻的矩阵元素不相等。
例子:
Input: mat[][] = { {2, 3, 4}, {2, 5, 4} }
Output: 3
Explanation: Decrease the matrix elements arr[0][0], arr[0][1], and arr[0][2] by 1 each. The matrix modifies to {{1, 2, 3}, {2, 5, 4}}. Therefore, all pairs of adjacent matrix elements becomes different.
Input: mat[][] = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3} }
Output: 3
Explanation: Decrease each element present in the second row of the matrix by 1. The matrix will have all adjacent element different as shown below:
1 2 3
0 1 2
1 2 3
方法:主要思想是通过假设矩阵如下图所示的国际象棋网格来解决给定的问题:
请按照以下步骤解决问题:
- 遍历矩阵
- 对于每个矩阵元素,都会出现以下两种情况:
- 情况1:如果(i + j)是偶数,则mat [i] [j]应该是偶数。否则, mat [i] [j]应该是奇数。
- 情况2:如果(i + j)为偶数,则mat [i] [j]上的值应为偶数。否则, mat [i] [j]的值应为奇数。
- 因此,请计算两种情况下所需的操作次数。
- 打印获得的两个计数中的最小值。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Matrix dimensions
const int n = 3;
const int m = 3;
// Function to count minimum
// number of operations required
void countDecrements(long long arr[][m])
{
int count_1 = 0;
int count_2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Case 1:
if ((i + j) % 2 == arr[i][j] % 2)
count_1++;
// Case 2:
if (1 - (i + j) % 2 == arr[i][j] % 2)
count_2++;
}
}
// Print the minimum number
// of operations required
cout << min(count_1, count_2);
}
// Driver Code
int main()
{
// The given matrix
long long arr[][m]
= { { 1, 2, 3 },
{ 1, 2, 3 },
{ 1, 2, 3 } };
// Function Call to count
// the minimum number of
// decrements required
countDecrements(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to count minimum
// number of operations required
static void countDecrements(long arr[][])
{
// Matrix dimensions
int n = arr.length;
int m = arr[0].length;
int count_1 = 0;
int count_2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Case 1:
if ((i + j) % 2 == arr[i][j] % 2)
count_1++;
// Case 2:
if (1 - (i + j) % 2 == arr[i][j] % 2)
count_2++;
}
}
// Print the minimum number
// of operations required
System.out.println(Math.min(count_1, count_2));
}
// Driver Code
public static void main(String[] args)
{
// The given matrix
long arr[][] = { { 1, 2, 3 },
{ 1, 2, 3 },
{ 1, 2, 3 } };
// Function Call to count
// the minimum number of
// decrements required
countDecrements(arr);
}
}
// This code is contributed by Kingash.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count minimum
// number of operations required
static void countDecrements(long[,] arr)
{
// Matrix dimensions
int n = arr.GetLength(0);
int m = arr.GetLength(1);
int count_1 = 0;
int count_2 = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
// Case 1:
if ((i + j) % 2 == arr[i, j] % 2)
count_1++;
// Case 2:
if (1 - (i + j) % 2 == arr[i, j] % 2)
count_2++;
}
}
// Print the minimum number
// of operations required
Console.WriteLine(Math.Min(count_1, count_2));
}
// Driver Code
public static void Main()
{
// The given matrix
long[,] arr = { { 1, 2, 3 },
{ 1, 2, 3 },
{ 1, 2, 3 } };
// Function Call to count
// the minimum number of
// decrements required
countDecrements(arr);
}
}
// This code is contributed by ukasp
Javascript
输出:
3
时间复杂度: O(N 2 )
辅助空间: O(1)