给定两个矩阵A和B,分别为pxq(或q X p)和rxs(或s X r)。任务是找到要添加到两个矩阵中的任何一个以使它们相乘的最小数量。
如果一个矩阵中的列数等于另一个矩阵中的行数,则两个矩阵是可乘的。
例子:
Input: p = 2, q = 3, r = 5, s = 6
Output: 4
Two columns can be added to make q = 5.
To add two columns, the number of elements will be added 2 * no. of rows i.e. 2 * 2 = 4
Input: p = 11, q = 5, r = 10, s = 11
Output: 0
方法:
- 如果一个矩阵中的列数等于另一矩阵中的行数,则不需要添加额外的元素。
- 如果它们不相等,则必须在一个矩阵中添加一些额外的列,或者在另一个矩阵中添加一些额外的行。
因此,计算两种情况下的额外元素数并打印最少的元素。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to calculate the minimum
// number of extra elements is to add
int minExtraElements(int p, int q, int r, int s)
{
// num1 will store minimum number of
// extra elements required
// to make A x B possible
// num2 will store minimum number of
// extra elements required
// to make B x A possible
int num1, num2;
// if either A x B or B x A is possible,
// it will return 0
if (q == r || p == s)
return 0;
else {
// it will calculate minimum number of
// extra elements required
// to make A x B possible
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
// it will calculate minimum number of
// extra elements required
// to make B x A possible
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
// return minimum of both
return min(num1, num2);
}
// Driver code
int main()
{
int p = 2, q = 3, r = 5, s = 6;
cout << minExtraElements(p, q, r, s) << endl;
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to calculate the minimum
// number of extra elements is to add
static int minExtraElements(int p, int q, int r, int s)
{
// num1 will store minimum number of
// extra elements required
// to make A x B possible
// num2 will store minimum number of
// extra elements required
// to make B x A possible
int num1, num2;
// if either A x B or B x A is possible,
// it will return 0
if (q == r || p == s)
return 0;
else {
// it will calculate minimum number of
// extra elements required
// to make A x B possible
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
// it will calculate minimum number of
// extra elements required
// to make B x A possible
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
// return minimum of both
return Math.min(num1, num2);
}
// Driver code
public static void main(String []rags)
{
int p = 2, q = 3, r = 5, s = 6;
System.out.println(minExtraElements(p, q, r, s));
}
}
// This code is contributed by ihritik
Python
# Python implementation of the above approach
# Function to calculate the minimum
# number of extra elements is to add
def minExtraElements(p, q, r, s):
# num1 will store minimum number of
# extra elements required
# to make A x B possible
# num2 will store minimum number of
# extra elements required
# to make B x A possible
# if either A x B or B x A is possible,
# it will return 0
if (q == r or p == s):
return 0
else :
# it will calculate minimum number of
# extra elements required
# to make A x B possible
if (q < r):
num1 = (r - q) * p
else:
num1 = (q - r) * s
# it will calculate minimum number of
# extra elements required
# to make B x A possible
if (p < s):
num2 = (s - p) * r
else:
num2 = (p - s) * q
# return minimum of both
return min(num1, num2)
# Driver code
p = 2
q = 3
r = 5
s = 6
print(minExtraElements(p, q, r, s))
# This code is contributed by ihritik
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to calculate the minimum
// number of extra elements is to add
static int minExtraElements(int p, int q, int r, int s)
{
// num1 will store minimum number of
// extra elements required
// to make A x B possible
// num2 will store minimum number of
// extra elements required
// to make B x A possible
int num1, num2;
// if either A x B or B x A is possible,
// it will return 0
if (q == r || p == s)
return 0;
else {
// it will calculate minimum number of
// extra elements required
// to make A x B possible
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
// it will calculate minimum number of
// extra elements required
// to make B x A possible
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
// return minimum of both
return Math.Min(num1, num2);
}
// Driver code
public static void Main()
{
int p = 2, q = 3, r = 5, s = 6;
Console.WriteLine(minExtraElements(p, q, r, s));
}
}
// This code is contributed by ihritik
PHP
Javascript
输出:
4
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。