在给定的矩阵中查找行和列最小值和最大值的乘积之间的绝对差
给定一个大小为N x N的方阵M[][] ,任务是找到每一行和每一列的最大值和最小值,将每一行对应的最大值和最小值乘以每一列的最大值和最小值,最后返回这两者的绝对差异。
例子:
Input: M[][] = [ [ 3, 2, 5 ], [ 7, 5, 4 ], [ 7, 2, 9 ] ]
Output: 11
Explanation:
Input: M = [ [1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Output: 40
朴素方法:可以通过模拟给定的操作来解决任务。请按照以下步骤解决问题:
- 取 2 个数组max1[ ]和min1[ ]用于存储每行或每列的最大值和最小值。
- 首先将每行的最大值存储在max1[ ]中,将每行的最小值存储在min1[ ]中。
- 多个矩阵和返回 res,将其存储在R中。
- 对矩阵M[ ]进行转置。
- 然后将每列的最大值存储在max1[ ]中,将每列的最小值存储在min1[ ]中。
- 将两个矩阵相乘并返回 res,将其存储在C中。
- 打印R和C的绝对差。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the transpose matrix M[]
vector> transpose(vector>M, int N){
for(int i = 0; i < N; i++)
{
for(int j = 0; j < i + 1; j++)
{
int temp = M[i][j];
M[i][j] = M[j][i];
M[j][i] = temp;
}
}
return M;
}
// Function to convert matrix M[][] to size 1 * 1
int matOne(vector>M, int N)
{
// Max1 and min1 to store max and min
// of each row pr column
vectormax1;
vectormin1;
// Loop to store element in max1 and min1
for(int r = 0; r < N; r++)
{
max1.push_back(*max_element(M[r].begin(), M[r].end()));
min1.push_back(*min_element(M[r].begin(), M[r].end()));
}
// Initialise res to 0
int res = 0;
// Multiply and add both array
for(int i = 0; i < N; i++){
res += max1[i]*min1[i];
}
// Return res
return res;
}
// Driver code
int main()
{
// Original matrix
vector>M = {{3, 2, 5},{7, 5, 4},{7, 2, 9}};
// N size of matrix
int N = M.size();
// Call matOne function for rows
int R = matOne(M, N);
// Transpose the matrix
vector>T = transpose(M, N);
// Call matOne function for column
int C = matOne(T, N);
// Print the absolute difference
cout << abs(R - C) << endl;
}
// This code is contributed by shinjanpatra
Python3
# Python program for the above approach
# Function to find the transpose matrix M[]
def transpose(M, N):
for i in range(N):
for j in range(i + 1):
M[i][j], M[j][i] = M[j][i], M[i][j]
return M
# Function to convert matrix M[][] to size 1 * 1
def matOne(M, N):
# Max1 and min1 to store max and min
# of each row pr column
max1 = []
min1 = []
# Loop to store element in max1 and min1
for r in M:
max1.append(max(r))
min1.append(min(r))
# Initialise res to 0
res = 0
# Multiply and add both array
for i in range(N):
res += max1[i]*min1[i]
# Return res
return res
# Driver code
if __name__ == "__main__":
# Original matrix
M = [[3, 2, 5],
[7, 5, 4],
[7, 2, 9]]
# N size of matrix
N = len(M)
# Call matOne function for rows
R = matOne(M, N)
# Transpose the matrix
T = transpose(M, N)
# Call matOne function for column
C = matOne(T, N)
# Print the absolute difference
print(str(abs(R-C)))
Javascript
Java
import java.util.Arrays;
// Java program for the above approach
class GFG
{
// Function to find the transpose matrix M[]
static int[][] transpose(int[][] M, int N) {
int transpose[][] = new int[N][N];
// Code to transpose a matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
transpose[i][j] = M[j][i];
}
}
return transpose;
}
// Function to convert matrix M[][] to size 1 * 1
static int matOne(int[][] M, int N) {
// Loop to calculate res.
int res = 0;
for (int[] r : M)
res += Arrays.stream(r).max().getAsInt() * Arrays.stream(r).min().getAsInt();
// Return res
return res;
}
// Driver code
public static void main(String[] args)
{
// Original matrix
int[][] M = { { 3, 2, 5 }, { 7, 5, 4 }, { 7, 2, 9 } };
// N size of matrix
int N = M.length;
// Call matOne function for rows
int R = matOne(M, N);
// Transpose the matrix
int[][] T = transpose(M, N);
// Call matOne function for column
int C = matOne(T, N);
// Print the absolute difference
System.out.println((Math.abs(R - C)));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the transpose matrix M[]
def transpose(M, N):
for i in range(N):
for j in range(i + 1):
M[i][j], M[j][i] = M[j][i], M[i][j]
return M
# Function to convert matrix M[][] to size 1 * 1
def matOne(M, N):
# Loop to calculate res.
res = 0
for r in M:
res += max(r)*min(r)
# Return res
return res
# Driver code
if __name__ == "__main__":
# Original matrix
M = [[3, 2, 5],
[7, 5, 4],
[7, 2, 9]]
# N size of matrix
N = len(M)
# Call matOne function for rows
R = matOne(M, N)
# Transpose the matrix
T = transpose(M, N)
# Call matOne function for column
C = matOne(T, N)
# Print the absolute difference
print(str(abs(R-C)))
C#
// C# program for the above approach
using System;
using System.Linq;
public class GFG
{
// Function to find the transpose matrix []M
static int[,] transpose(int[,] M, int N)
{
int [,]transpose = new int[N,N];
// Code to transpose a matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
transpose[i,j] = M[j,i];
}
}
return transpose;
}
// Function to convert matrix [,]M to size 1 * 1
static int matOne(int[,] M, int N) {
// Loop to calculate res.
int res = 0;
for (int r =0;r< N;r++){
int[] t = new int[N];
for(int j =0;j
Javascript
输出:
11
时间复杂度: 在)
辅助空间: 在)
空间优化方法:这种方法与方法 1 类似,但在此方法中,将不使用 max1 和 min1 数组来优化辅助空间。在此,直接多并添加元素并返回它。
下面是上述方法的实现:
Java
import java.util.Arrays;
// Java program for the above approach
class GFG
{
// Function to find the transpose matrix M[]
static int[][] transpose(int[][] M, int N) {
int transpose[][] = new int[N][N];
// Code to transpose a matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
transpose[i][j] = M[j][i];
}
}
return transpose;
}
// Function to convert matrix M[][] to size 1 * 1
static int matOne(int[][] M, int N) {
// Loop to calculate res.
int res = 0;
for (int[] r : M)
res += Arrays.stream(r).max().getAsInt() * Arrays.stream(r).min().getAsInt();
// Return res
return res;
}
// Driver code
public static void main(String[] args)
{
// Original matrix
int[][] M = { { 3, 2, 5 }, { 7, 5, 4 }, { 7, 2, 9 } };
// N size of matrix
int N = M.length;
// Call matOne function for rows
int R = matOne(M, N);
// Transpose the matrix
int[][] T = transpose(M, N);
// Call matOne function for column
int C = matOne(T, N);
// Print the absolute difference
System.out.println((Math.abs(R - C)));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the transpose matrix M[]
def transpose(M, N):
for i in range(N):
for j in range(i + 1):
M[i][j], M[j][i] = M[j][i], M[i][j]
return M
# Function to convert matrix M[][] to size 1 * 1
def matOne(M, N):
# Loop to calculate res.
res = 0
for r in M:
res += max(r)*min(r)
# Return res
return res
# Driver code
if __name__ == "__main__":
# Original matrix
M = [[3, 2, 5],
[7, 5, 4],
[7, 2, 9]]
# N size of matrix
N = len(M)
# Call matOne function for rows
R = matOne(M, N)
# Transpose the matrix
T = transpose(M, N)
# Call matOne function for column
C = matOne(T, N)
# Print the absolute difference
print(str(abs(R-C)))
C#
// C# program for the above approach
using System;
using System.Linq;
public class GFG
{
// Function to find the transpose matrix []M
static int[,] transpose(int[,] M, int N)
{
int [,]transpose = new int[N,N];
// Code to transpose a matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
transpose[i,j] = M[j,i];
}
}
return transpose;
}
// Function to convert matrix [,]M to size 1 * 1
static int matOne(int[,] M, int N) {
// Loop to calculate res.
int res = 0;
for (int r =0;r< N;r++){
int[] t = new int[N];
for(int j =0;j
Javascript
输出:
11
时间复杂度: 在)
辅助空间: O(1)