将给定矩阵转换为对角优势矩阵的最小步骤数
给定一个NxN阶矩阵,任务是找到将给定矩阵转换为对角优势矩阵的最少步数。在每一步中,唯一允许的操作是将任何元素减少或增加 1。
例子:
Input: mat[][] = {{3, 2, 4}, {1, 4, 4}, {2, 3, 4}}
Output: 5
Sum of the absolute values of elements of row 1 except
the diagonal element is 3 more than abs(arr[0][0]).
1 more than abs(arr[1][1]) in the second row
and 1 more than abs(arr[2][2]) in the third row.
Hence, 3 + 1 + 1 = 5
Input: mat[][] = {{1, 2, 4, 0}, {1, 3, 4, 2}, {3, 3, 4, 2}, {-1, 0, 1, 4}}
Output: 13
方法:
- 如果对于矩阵的每一行,该方阵中的对角线项的幅值大于或等于该行中所有其他(非对角线)项的幅值之和,则称该方阵为对角占优矩阵。排。
- 可以根据两种情况计算将给定矩阵转换为对角主导矩阵所需的最小步骤数:
- 如果一行中除对角线元素外的所有元素的绝对值之和大于对角线元素的绝对值,则将这两个值之间的差值加到结果中。
- 否则无需在结果中添加任何内容,因为在这种情况下,行满足对角占优矩阵的条件。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define N 3
// Function to return the minimum steps
// required to convert the given matrix
// to a Diagonally Dominant Matrix
int findStepsForDDM(int arr[][N])
{
int result = 0;
// For each row
for (int i = 0; i < N; i++) {
// To store the sum of the current row
int sum = 0;
for (int j = 0; j < N; j++)
sum += abs(arr[i][j]);
// Remove the element of the current row
// which lies on the main diagonal
sum -= abs(arr[i][i]);
// Checking if the diagonal element is less
// than the sum of non-diagonal element
// then add their difference to the result
if (abs(arr[i][i]) < abs(sum))
result += abs(abs(arr[i][i]) - abs(sum));
}
return result;
}
// Driven code
int main()
{
int arr[N][N] = { { 3, -2, 1 },
{ 1, -3, 2 },
{ -1, 2, 4 } };
cout << findStepsForDDM(arr);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
final static int N = 3 ;
// Function to return the minimum steps
// required to convert the given matrix
// to a Diagonally Dominant Matrix
static int findStepsForDDM(int arr[][])
{
int result = 0;
// For each row
for (int i = 0; i < N; i++)
{
// To store the sum of the current row
int sum = 0;
for (int j = 0; j < N; j++)
sum += Math.abs(arr[i][j]);
// Remove the element of the current row
// which lies on the main diagonal
sum -= Math.abs(arr[i][i]);
// Checking if the diagonal element is less
// than the sum of non-diagonal element
// then add their difference to the result
if (Math.abs(arr[i][i]) < Math.abs(sum))
result += Math.abs(Math.abs(arr[i][i]) - Math.abs(sum));
}
return result;
}
// Driven code
public static void main (String[] args)
{
int arr[][] = { { 3, -2, 1 },
{ 1, -3, 2 },
{ -1, 2, 4 } };
System.out.println(findStepsForDDM(arr));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
N = 3
# Function to return the minimum steps
# required to convert the given matrix
# to a Diagonally Dominant Matrix
def findStepsForDDM(arr):
result = 0
# For each row
for i in range(N):
# To store the sum of the current row
sum = 0
for j in range(N):
sum += abs(arr[i][j])
# Remove the element of the current row
# which lies on the main diagonal
sum -= abs(arr[i][i])
# Checking if the diagonal element is less
# than the sum of non-diagonal element
# then add their difference to the result
if (abs(arr[i][i]) < abs(sum)):
result += abs(abs(arr[i][i]) - abs(sum))
return result
# Driver code
arr= [ [ 3, -2, 1 ],
[ 1, -3, 2 ],
[ -1, 2, 4 ] ]
print(findStepsForDDM(arr))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
static int N = 3 ;
// Function to return the minimum steps
// required to convert the given matrix
// to a Diagonally Dominant Matrix
static int findStepsForDDM(int [,]arr)
{
int result = 0;
// For each row
for (int i = 0; i < N; i++)
{
// To store the sum of the current row
int sum = 0;
for (int j = 0; j < N; j++)
sum += Math.Abs(arr[i,j]);
// Remove the element of the current row
// which lies on the main diagonal
sum -= Math.Abs(arr[i,i]);
// Checking if the diagonal element is less
// than the sum of non-diagonal element
// then add their difference to the result
if (Math.Abs(arr[i,i]) < Math.Abs(sum))
result += Math.Abs(Math.Abs(arr[i,i]) - Math.Abs(sum));
}
return result;
}
// Driven code
static public void Main ()
{
int [,]arr = { { 3, -2, 1 },
{ 1, -3, 2 },
{ -1, 2, 4 } };
Console.WriteLine(findStepsForDDM(arr));
}
}
// This code is contributed by ajit.
Javascript
输出:
0
时间复杂度: O(N 2 )
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。