📌  相关文章
📜  从给定点集中平行于X和Y轴的线形成的最大矩形的面积

📅  最后修改于: 2021-04-24 18:05:32             🧑  作者: Mango

给定一个数组nrr [] ,该数组arr []由代表N个点的坐标的N对整数组成,任务是找到由平行于给定点集的XY轴绘制的直线形成的最大矩形的面积。

例子:

方法:可以使用排序技术解决问题。请按照以下步骤解决问题:

  1. XY坐标存储在两个不同的数组中,例如x []y []
  2. 对数组x []y []进行排序。
  3. 找到两个数组的最大相邻差并将其存储在变量X_MaxY_Max中。
  4. 可能的最大矩形区域是X_MaxY_Max的乘积。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return the area of the largest
// rectangle formed by lines parallel to X
// and Y axis from given set of points
int maxRectangle(vector > sequence,
                 int size)
{
    // Initialize two arrays
    long long int X_Cord[size], Y_Cord[size];
 
    // Store x and y coordinates
    for (int i = 0; i < size; i++) {
        X_Cord[i] = sequence[i][0];
        Y_Cord[i] = sequence[i][1];
    }
 
    // Sort arrays
    sort(X_Cord, X_Cord + size);
    sort(Y_Cord, Y_Cord + size);
 
    // Initialize max differences
    long long int X_Max = 0, Y_Max = 0;
 
    // Find max adjacent differences
    for (int i = 0; i < size - 1; i++) {
        X_Max = max(X_Max, X_Cord[i + 1]
                               - X_Cord[i]);
        Y_Max = max(Y_Max, Y_Cord[i + 1]
                               - Y_Cord[i]);
    }
 
    // Return answer
    return X_Max * Y_Max;
}
 
// Driver Code
int main()
{
    // Given points
    vector > point
        = { { -2, 0 }, { 2, 0 }, { 4, 0 }, { 4, 2 } };
 
    // Total points
    int n = point.size();
 
    // Function call
    cout << maxRectangle(point, n);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to return the area of the largest
// rectangle formed by lines parallel to X
// and Y axis from given set of points
static int maxRectangle(int[][] sequence,
                        int size)
{
     
    // Initialize two arrays
    int[] X_Cord = new int[size];
    int[] Y_Cord = new int[size];
 
    // Store x and y coordinates
    for(int i = 0; i < size; i++)
    {
        X_Cord[i] = sequence[i][0];
        Y_Cord[i] = sequence[i][1];
    }
 
    // Sort arrays
    Arrays.sort(X_Cord);
    Arrays.sort(Y_Cord);
 
    // Initialize max differences
    int X_Max = 0, Y_Max = 0;
 
    // Find max adjacent differences
    for(int i = 0; i < size - 1; i++)
    {
        X_Max = Math.max(X_Max, X_Cord[i + 1] -
                                X_Cord[i]);
        Y_Max = Math.max(Y_Max, Y_Cord[i + 1] -
                                Y_Cord[i]);
    }
     
    // Return answer
    return X_Max * Y_Max;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given points
    int[][] point = { { -2, 0 }, { 2, 0 },
                      { 4, 0 }, { 4, 2 } };
 
    // Total points
    int n = point.length;
 
    // Function call
    System.out.print(maxRectangle(point, n));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the
# above approach
 
# Function to return the
# area of the largest
# rectangle formed by lines
# parallel to X and Y axis
# from given set of points
def maxRectangle(sequence, size):
 
    # Initialize two arrays
    X_Cord = [0] * size
    Y_Cord = [0] * size
 
    # Store x and y coordinates
    for i in range(size):
        X_Cord[i] = sequence[i][0]
        Y_Cord[i] = sequence[i][1]
 
    # Sort arrays
    X_Cord.sort()
    Y_Cord.sort()
 
    # Initialize max differences
    X_Max = 0
    Y_Max = 0
 
    # Find max adjacent differences
    for i in range(size - 1):
        X_Max = max(X_Max,
                    X_Cord[i + 1] -
                    X_Cord[i])
        Y_Max = max(Y_Max,
                    Y_Cord[i + 1] -
                    Y_Cord[i])
 
    # Return answer
    return X_Max * Y_Max
 
# Driver Code
if __name__ == "__main__":
   
    # Given points
    point = [[-2, 0], [2, 0],
             [4, 0], [4, 2]]
 
    # Total points
    n = len(point)
 
    # Function call
    print(maxRectangle(point, n))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to return the area of the largest
// rectangle formed by lines parallel to X
// and Y axis from given set of points
static int maxRectangle(int[,] sequence,
                        int size)
{
     
    // Initialize two arrays
    int[] X_Cord = new int[size];
    int[] Y_Cord = new int[size];
     
    // Store x and y coordinates
    for(int i = 0; i < size; i++)
    {
        X_Cord[i] = sequence[i, 0];
        Y_Cord[i] = sequence[i, 1];
    }
 
    // Sort arrays
    Array.Sort(X_Cord);
    Array.Sort(Y_Cord);
 
    // Initialize max differences
    int X_Max = 0, Y_Max = 0;
 
    // Find max adjacent differences
    for(int i = 0; i < size - 1; i++)
    {
        X_Max = Math.Max(X_Max, X_Cord[i + 1] -
                                X_Cord[i]);
        Y_Max = Math.Max(Y_Max, Y_Cord[i + 1] -
                                Y_Cord[i]);
    }
     
    // Return answer
    return X_Max * Y_Max;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given points
    int[,] point = { { -2, 0 }, { 2, 0 },
                     { 4, 0 }, { 4, 2 } };
 
    // Total points
    int n = point.GetLength(0);
 
    // Function call
    Console.Write(maxRectangle(point, n));
}
}
 
// This code is contributed by shikhasingrajput


输出:
8

时间复杂度: O(N * log(N)),其中N是总点数。
辅助空间: O(N)