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

📅  最后修改于: 2021-10-23 08:52:44             🧑  作者: Mango

给定一个由N对整数组成的数组arr[] ,表示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


Javascript


输出:
8

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程