鉴于使用数字1的大小N * N的方矩阵,以N ^ 2,任务是找到最大的矩阵的每一层的最小值的。
The layers of the matrix are the boundary elements of the sub-matrix starting at (i, i) and ending at (N – i + 1, N – i + 1), where 1<= i<= ceil(N/2).
例子:
Input: Below is the given matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output: 6
Explanation: The layers are {1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5} with minimum 1 and {6, 7, 10, 11} with minimum 6. The maximum of 1 and 6 is 6.
Input: Below is the given matrix:
1 2 3
4 30 5
1 2 3
Output: 30
Explanation: The layers are {1, 2, 3, 5, 3, 2, 1, 4, 1} with minimum 1 and {30} with minimum 30. The maximum of 1 and 30 is 30.
方法:我们的想法是要细心观察,为第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现场课程美国》。