📌  相关文章
📜  使用数字1到N ^ 2从矩阵层中找到最小值的最大值

📅  最后修改于: 2021-06-26 02:49:15             🧑  作者: Mango

鉴于使用数字1的大小N * N的方矩阵,以N ^ 2,任务是找到最大的矩阵的每一层的最小值的。

例子:

方法:我们的想法是要细心观察,为第i层,顶部的元素,左,右,下边界是在指标:

  • 最高边界位于索引(i,j)
  • 左边界位于索引(j,i)
  • 右边界位于索引(j,n – i + 1)
  • 底部边界位于索引(n – i + 1,j),其中i <= j <= n – i + 1

因此,找到每一层的最小值并存储这些最小值中的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
  
// Function to return the minimum
// of the boundary elements
int getBoundaryMin(int a[][4], int n,
                   int index)
{
    int min1 = INT_MAX;
  
    for(int i = index; i < n - index; i++) 
    {
          
        // Top boundary
        min1 = min(min1,
                   a[index][i]);
  
        // Left boundary
        min1 = min(min1,
                   a[i][index]);
  
        // Right boundary
        min1 = min(min1,
                    a[i][n - index - 1]);
  
        // Bottom boundary
        min1 = min(min1,
                   a[n - index - 1][i]);
    }
    return min1;
}
  
// Function to find the maximum of
// minimums of all layers
void MaximumOfMinimum(int a[][4], int n)
{
      
    // Calculate the layers
    int layers = n / 2 + n % 2;
    int max1 = INT_MIN;
  
    // Iterate for all the layers
    for(int i = 0; i < layers; i++)
    {
          
        // Find maximum
        max1 = max(max1,
                   getBoundaryMin(a, n, i));
    }
      
    // Print the answer
    cout << (max1);
}
  
// Driver Code
int main()
{
      
    // Initialize the matrix
    int a[][4] = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 10, 11, 12 },
                   { 13, 14, 15, 16 } };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    // Function call
    MaximumOfMinimum(a, n);
}
  
// This code is contributed by chitranayal


Java
// Java Program for the above approach
  
class GFG {
  
    // Function to return the minimum
    // of the boundary elements
    static int
    getBoundaryMin(int a[][], int n,
                   int index)
    {
        int min = Integer.MAX_VALUE;
  
        for (int i = index; i < n - index; i++) {
  
            // Top boundary
            min = Math.min(
                min,
                a[index][i]);
  
            // Left boundary
            min = Math.min(
                min,
                a[i][index]);
  
            // Right boundary
            min = Math.min(
                min,
                a[i][n - index - 1]);
  
            // Bottom boundary
            min = Math.min(
                min,
                a[n - index - 1][i]);
        }
  
        return min;
    }
  
    // Function to find the maximum of
    // minimums of all layers
    static void MaximumOfMinimum(
        int a[][], int n)
    {
        // Calculate the layers
        int layers = n / 2 + n % 2;
        int max = Integer.MIN_VALUE;
  
        // Iterate for all the layers
        for (int i = 0; i < layers; i++) {
  
            // Find maximum
            max
                = Math.max(
                    max,
                    getBoundaryMin(a, n, i));
        }
  
        // Print the answer
        System.out.print(max);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Initialize the matrix
        int a[][] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
  
        int n = a.length;
  
        // Function call
        MaximumOfMinimum(a, n);
    }
}


Python3
# Python3 program for the above approach
import sys
  
# Function to return the minimum
# of the boundary elements
def getBoundaryMin(a, n, index):
      
    min1 = sys.maxsize
  
    for i in range(index, n - index):
          
        # Top boundary
        min1 = min(min1, a[index][i])
  
        # Left boundary
        min1 = min(min1, a[i][index])
  
        # Right boundary
        min1 = min(min1, a[i][n - index - 1])
  
        # Bottom boundary
        min1 = min(min1, a[n - index - 1][i])
      
    return min1
  
# Function to find the maximum of
# minimums of all layers
def MaximumOfMinimum(a, n):
      
    # Calculate the layers
    layers = n // 2 + n % 2
    max1 = -sys.maxsize - 1
  
    # Iterate for all the layers
    for i in range(0, layers):
          
        # Find maximum
        max1 = max(max1, getBoundaryMin(a, n, i))
      
    # Print the answer
    print(max1)
  
# Driver Code
      
# Initialize the matrix
a = [ [ 1, 2, 3, 4 ],
      [ 5, 6, 7, 8 ],
      [ 9, 10, 11, 12 ] ,
      [ 13, 14, 15, 16 ] ] 
  
n = len(a)
  
# Function call
MaximumOfMinimum(a, n)
  
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to return the minimum
// of the boundary elements
static int getBoundaryMin(int[,]a, int n,
                          int index)
{
    int min = int.MaxValue;
  
    for(int i = index; i < n - index; i++) 
    {
          
        // Top boundary
        min = Math.Min(min, a[index, i]);
  
        // Left boundary
        min = Math.Min(min, a[i, index]);
  
        // Right boundary
        min = Math.Min(min, a[i, n - index - 1]);
  
        // Bottom boundary
        min = Math.Min(min, a[n - index - 1, i]);
    }
    return min;
}
  
// Function to find the maximum of
// minimums of all layers
static void MaximumOfMinimum(int[,]a, int n)
{
      
    // Calculate the layers
    int layers = n / 2 + n % 2;
    int max = int.MinValue;
  
    // Iterate for all the layers
    for(int i = 0; i < layers; i++) 
    {
          
        // Find maximum
        max = Math.Max(max,
                       getBoundaryMin(a, n, i));
    }
  
    // Print the answer
    Console.Write(max);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Initialize the matrix
    int[,]a = { { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 },
                { 13, 14, 15, 16 } };
  
    int n = a.GetLength(0);
  
    // Function call
    MaximumOfMinimum(a, n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
6

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。