📌  相关文章
📜  最大化由给定矩阵的两行的相同索引元素的最大值生成的数组的最小值

📅  最后修改于: 2021-05-05 01:58:52             🧑  作者: Mango

给定N行和M列的矩阵mat [] [] ,任务是选择任意两行(i,j)(0≤i,j≤N – 1)并构造一个新的大小为M的数组A [] ,其中A [k] = max(mat [i] [k],mat [j] [k]) ,以使A []的最小值成为最大可能。

例子:

方法:请按照以下步骤解决问题:

  • 使用INT_MAX初始化变量global_max ,该变量存储从mat [] []的任意两行构造的最小数组的最大值。
  • 遍历行ij的每种可能组合,并找到由它们构成的最小数组row_min。
  • 在每次迭代中,将global_max更新为row_min及其自身的最大值。
  • 完成上述步骤后,输出global_max的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum of
// minimum of array constructed from
// any two rows of the given matrix
int getMaximum(int N, int M,
               vector > mat)
{
    // Initialize global max as INT_MIN
    int global_max = INT_MIN;
 
    // Iterate through the rows
    for (int i = 0; i < N; i++) {
 
        // Iterate through remaining rows
        for (int j = i + 1; j < N; j++) {
 
            // Initialize row_min as INT_MAX
            int row_min = INT_MAX;
 
            // Iterate through the column
            // values of two rows
            for (int k = 0; k < M; k++) {
 
                // Find max of two elements
                int m = max(mat[i][k],
                            mat[j][k]);
 
                // Update the row_min
                row_min = min(row_min, m);
            }
 
            // Update the global_max
            global_max = max(global_max,
                             row_min);
        }
    }
 
    // Print the global max
    return global_max;
}
 
// Driver Code
int main()
{
 
    // Given matrix mat[][]
    vector > mat
        = { { 5, 0, 3, 1, 2 },
            { 1, 8, 9, 1, 3 },
            { 1, 2, 3, 4, 5 },
            { 9, 1, 0, 3, 7 },
            { 2, 3, 0, 6, 3 },
            { 6, 4, 1, 7, 0 } };
 
    // Given number of rows and columns
    int N = 6, M = 5;
 
    // Function Call
    cout << getMaximum(N, M, mat);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum of
// minimum of array constructed from
// any two rows of the given matrix
static int getMaximum(int N, int M, int[][] mat)
{
     
    // Initialize global max as INT_MIN
    int global_max = Integer.MIN_VALUE;
     
    // Iterate through the rows
    for(int i = 0; i < N; i++)
    {
         
        // Iterate through remaining rows
        for(int j = i + 1; j < N; j++)
        {
             
            // Initialize row_min as INT_MAX
            int row_min = Integer.MAX_VALUE;
             
            // Iterate through the column
            // values of two rows
            for(int k = 0; k < M; k++)
            {
                 
                // Find max of two elements
                int m = Math.max(mat[i][k],
                                 mat[j][k]);
 
                // Update the row_min
                row_min = Math.min(row_min, m);
            }
 
            // Update the global_max
            global_max = Math.max(global_max,
                                  row_min);
        }
    }
 
    // Print the global max
    return global_max;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given matrix mat[][]
    int[][] mat = { { 5, 0, 3, 1, 2 },
                    { 1, 8, 9, 1, 3 },
                    { 1, 2, 3, 4, 5 },
                    { 9, 1, 0, 3, 7 },
                    { 2, 3, 0, 6, 3 },
                    { 6, 4, 1, 7, 0 } };
 
    // Given number of rows and columns
    int N = 6, M = 5;
 
    // Function Call
    System.out.println(getMaximum(N, M, mat));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
import sys
 
# Function to find the maximum of
# minimum of array constructed from
# any two rows of the given matrix
def getMaximum(N, M, mat):
     
    # Initialize global max as INT_MIN
    global_max = -1 * (sys.maxsize)
 
    # Iterate through the rows
    for i in range(0, N):
         
        # Iterate through remaining rows
        for j in range(i + 1, N):
 
            # Initialize row_min as INT_MAX
            row_min = sys.maxsize
 
            # Iterate through the column
            # values of two rows
            for k in range(0, M):
 
                # Find max of two elements
                m = max(mat[i][k], mat[j][k])
 
                # Update the row_min
                row_min = min(row_min, m)
 
            # Update the global_max
            global_max = max(global_max,
                             row_min)
 
    # Print the global max
    return global_max
 
# Driver Code
if __name__ == '__main__':
 
    # Given matrix mat[][]
    mat = [ [ 5, 0, 3, 1, 2 ],
            [ 1, 8, 9, 1, 3 ],
            [ 1, 2, 3, 4, 5 ],
            [ 9, 1, 0, 3, 7 ],
            [ 2, 3, 0, 6, 3 ],
            [ 6, 4, 1, 7, 0 ] ]
 
    # Given number of rows and columns
    N = 6
    M = 5
 
    # Function Call
    print(getMaximum(N, M, mat))
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum of
// minimum of array constructed from
// any two rows of the given matrix
static int getMaximum(int N, int M, int[,] mat)
{
     
    // Initialize global max as INT_MIN
    int global_max = int.MinValue;
     
    // Iterate through the rows
    for(int i = 0; i < N; i++)
    {
         
        // Iterate through remaining rows
        for(int j = i + 1; j < N; j++)
        {
             
            // Initialize row_min as INT_MAX
            int row_min = int.MaxValue;
 
            // Iterate through the column
            // values of two rows
            for(int k = 0; k < M; k++)
            {
                 
                // Find max of two elements
                int m = Math.Max(mat[i, k],
                                 mat[j, k]);
                                  
                // Update the row_min
                row_min = Math.Min(row_min, m);
            }
 
            // Update the global_max
            global_max = Math.Max(global_max,
                                  row_min);
        }
    }
 
    // Print the global max
    return global_max;
}
 
// Driver Code
public static void Main()
{
     
    // Given matrix mat[][]
    int[,] mat = { { 5, 0, 3, 1, 2 },
                   { 1, 8, 9, 1, 3 },
                   { 1, 2, 3, 4, 5 },
                   { 9, 1, 0, 3, 7 },
                   { 2, 3, 0, 6, 3 },
                   { 6, 4, 1, 7, 0 } };
 
    // Given number of rows and columns
    int N = 6, M = 5;
 
    // Function Call
    Console.WriteLine(getMaximum(N, M, mat));
}
}
 
// This code is contributed by akhilsaini


输出:
3

时间复杂度: O(M * N 2 )
辅助空间: O(1)