📜  将 Array 中的 N 对作为 (X, Y) 坐标点,它们包含在最小面积矩形内

📅  最后修改于: 2022-05-13 01:56:05.582000             🧑  作者: Mango

将 Array 中的 N 对作为 (X, Y) 坐标点,它们包含在最小面积矩形内

给定一个数字N和一个大小为2N的数组A[] ,任务是制作N对这些数组元素并将它们放置在XY坐标平面上,这样它们就被包围在一个最小面积矩形内(边平行于X轴和Y轴)并打印矩形的面积。

例子:

方法:左下角在(X 1 , Y 1 )和右上角在 ( X 2 , Y 2 )的矩形的面积将是(X 2 – X 1 )*(Y 2 – Y 1 )。因此,该任务可以表示为将数组A划分为两个N 大小的分区,例如XY ,使得(Max(X) – Min(X)) * (Max(Y) – Min(Y))最小化.这里, X表示对的X 坐标Y表示Y 坐标。

排序A后,最小值为A 1 ,最大值为A 2N 现在,可能有以下两种情况:

  • A 1A 2N都存在于同一个分区中,比如X 。矩形的面积将是(A 2N – A 1 ) * (Max(Y) – Min(Y))。然后任务是最小化Max(Y) – Min(Y)。此外,如果iMin(Y)的索引, jMax(Y) 的索引,j – i >= N – 1 ,因为Y中必须至少有N个元素。因此,对Y使用大小为N的段是最佳的(除了A 1A 2N ,因为它们已经被采用了),
  • A 1A 2N存在于不同的分区中。对于这种情况,最好使用大小为N的前缀和大小为N的后缀作为分区,即将前N个元素放在一个分区中,将最后N个元素放在另一个分区中。      

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

  • 初始化一个变量say, ans来存储矩形的最小面积。
  • 按升序对数组A[]进行排序。
  • 对于第一种情况
    • ans更新为(A[N – 1] – A[0]) * (A[2 * N – 1] – A[N])。
  • 对于第二种情况
    • 使用变量i[1, N-1]范围内迭代:  
      • ans更新为ans(A[2 * N – 1] – A[0]) * (A[i + N – 1] – A[i]) 的最小值。
  • 最后,返回ans。

下面是上述代码的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to make N pairs of coordinates such that they
// are enclosed in a minimum area rectangle with sides
// parallel to the X and Y axes
int minimumRectangleArea(int A[], int N)
{
    // A variable to store the answer
    int ans;
 
    sort(A, A + 2 * N);
 
    // For the case where the maximum
    // and minimum are in different partitions
    ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);
 
    // For the case where the maximum and
    // minimum are in the same partition
    for (int i = 1; i < N; i++)
        ans = min(ans, (A[2 * N - 1] - A[0])
                           * (A[i + N - 1] - A[i]));
 
    // Return the answer
    return ans;
}
 
// Driver code
int main()
{
    // Given Input
    int A[] = { 2, 4, 1, 5, 3, 6, 7, 8 };
    int N = sizeof(A) / sizeof(A[0]);
    N /= 2;
 
    // Function call
    cout << minimumRectangleArea(A, N) << endl;
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
 
class GFG{
     
// Function to make N pairs of coordinates
// such that they are enclosed in a minimum
// area rectangle with sides parallel to
// the X and Y axes
public static int minimumRectangleArea(int A[], int N)
{
     
    // A variable to store the answer
    int ans;
 
    Arrays.sort(A);
 
    // For the case where the maximum
    // and minimum are in different partitions
    ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);
 
    // For the case where the maximum and
    // minimum are in the same partition
    for(int i = 1; i < N; i++)
        ans = Math.min(ans, (A[2 * N - 1] - A[0]) *
                            (A[i + N - 1] - A[i]));
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    int A[] = { 2, 4, 1, 5, 3, 6, 7, 8 };
    int N = A.length;
    N = (int)N / 2;
 
    // Function call
    System.out.println(minimumRectangleArea(A, N));
}
}
 
// This code is contributed by lokeshpotta20


Python3
# Python3 program for the above approach
 
# Function to make N pairs of coordinates
# such that they are enclosed in a minimum
# area rectangle with sides parallel to the
# X and Y axes
def minimumRectangleArea(A, N):
     
    # A variable to store the answer
    ans = 0
 
    A.sort()
 
    # For the case where the maximum
    # and minimum are in different partitions
    ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N])
 
    # For the case where the maximum and
    # minimum are in the same partition
    for i in range(1, N, 1):
        ans = min(ans, (A[2 * N - 1] - A[0]) *
                       (A[i + N - 1] - A[i]))
 
    # Return the answer
    return ans
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    A = [ 2, 4, 1, 5, 3, 6, 7, 8 ]
    N = len(A)
    N //= 2
 
    # Function call
    print(minimumRectangleArea(A, N))
     
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to make N pairs of coordinates
// such that they are enclosed in a minimum
// area rectangle with sides parallel to
// the X and Y axes
public static int minimumRectangleArea(int []A, int N)
{
     
    // A variable to store the answer
    int ans;
 
    Array.Sort(A);
 
    // For the case where the maximum
    // and minimum are in different partitions
    ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);
 
    // For the case where the maximum and
    // minimum are in the same partition
    for(int i = 1; i < N; i++)
        ans = Math.Min(ans, (A[2 * N - 1] - A[0]) *
                            (A[i + N - 1] - A[i]));
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given Input
    int []A = { 2, 4, 1, 5, 3, 6, 7, 8 };
    int N = A.Length;
    N = (int)N / 2;
 
    // Function call
    Console.Write(minimumRectangleArea(A, N));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
9

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