📜  具有至少一个公共点的重叠矩形的最大数量

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

具有至少一个公共点的重叠矩形的最大数量

给定四个数组x1[] , x2[] , y1[] , y2[]大小为N其中 ( x1[i] , y1[i] ) 表示左下角并且 ( x2[i] , y2[i] ) 矩形的右上角,任务是找到至少有一个公共点的重叠矩形的最大数量。

例子:

方法:给定的问题可以通过使用贪心方法来解决,该方法基于以下思想:坐标平面上的每个矩形都有自己的区域,当在同一平面上添加多个矩形时,它们会在彼此之间产生交集。因此,要选择在公共区域上重叠的最大矩形数量,贪婪地选择1×1 单位的区域,因为所有重叠区域都至少有这么多块。请按照以下步骤解决给定的问题:

  • 因为有N个矩形,每个矩形有2 个 X 坐标2 个 Y 坐标。总共将有2*NX -coordinatesY-coordinates
  • 因此,创建一个XY坐标向量,并将所有XY从各自向量中的矩形推入。
  • 初始化一个变量,例如maxRectangles0 ,它存储最大重叠矩形。
  • 遍历向量X[]并且对于每个坐标X[i] ,遍历向量Y[]并找到与X[i]重叠的矩形的数量,在这一步之后,将maxRectangles的值更新为maxRectangles的最大值并计数为当前迭代获得。
  • 完成上述步骤后,打印maxRectangles的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum number
// of overlapping rectangles
void maxOverlappingRectangles(
    int x1[], int y1[], int x2[],
    int y2[], int N)
{
    // Stores the maximum count of
    // overlapping rectangles
    int max_rectangles = 0;
 
    // Stores the X and Y coordinates
    vector X, Y;
 
    for (int i = 0; i < N; i++) {
        X.push_back(x1[i]);
        X.push_back(x2[i] - 1);
        Y.push_back(y1[i]);
        Y.push_back(y2[i] - 1);
    }
 
    // Iterate over all pairs of Xs and Ys
    for (int i = 0; i < X.size(); i++) {
 
        for (int j = 0; j < Y.size(); j++) {
 
            // Store the count for the
            // current X and Y
            int cnt = 0;
            for (int k = 0; k < N; k++) {
 
                if (X[i] >= x1[k]
                    && X[i] + 1 <= x2[k]
                    && Y[j] >= y1[k]
                    && Y[j] + 1 <= y2[k]) {
                    cnt++;
                }
            }
 
            // Update the maximum count of
            // rectangles
            max_rectangles = max(
                max_rectangles, cnt);
        }
    }
 
    // Returns the total count
    cout << max_rectangles;
}
 
// Driver Code
int main()
{
    int x1[] = { 0, 50 };
    int y1[] = { 0, 50 };
    int x2[] = { 100, 60 };
    int y2[] = { 100, 60 };
    int N = sizeof(x1) / sizeof(x1[0]);
 
    maxOverlappingRectangles(
        x1, y1, x2, y2, N);
 
    return 0;
}


Java
// java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find the maximum number
  // of overlapping rectangles
  static void maxOverlappingRectangles(int[] x1, int[] y1,
                                       int[] x2, int[] y2,
                                       int N)
  {
 
    // Stores the maximum count of
    // overlapping rectangles
    int max_rectangles = 0;
 
    // Stores the X and Y coordinates
    Vector X = new Vector<>();
    Vector Y = new Vector<>();
 
    for (int i = 0; i < N; i++) {
      X.add(x1[i]);
      X.add(x2[i] - 1);
      Y.add(y1[i]);
      Y.add(y2[i] - 1);
    }
 
    // Iterate over all pairs of Xs and Ys
    for (int i = 0; i < X.size(); i++) {
 
      for (int j = 0; j < Y.size(); j++) {
 
        // Store the count for the
        // current X and Y
        int cnt = 0;
        for (int k = 0; k < N; k++) {
 
          if (X.get(i)>= x1[k] && X.get(i) + 1 <= x2[k]
              && Y.get(j) >= y1[k]
              && Y.get(j) + 1 <= y2[k]) {
            cnt++;
          }
        }
 
        // Update the maximum count of
        // rectangles
        max_rectangles
          = Math.max(max_rectangles, cnt);
      }
    }
 
    // Returns the total count
    System.out.println(max_rectangles);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] x1 = { 0, 50 };
    int[] y1 = { 0, 50 };
    int[] x2 = { 100, 60 };
    int[] y2 = { 100, 60 };
    int N = x1.length;
 
    maxOverlappingRectangles(x1, y1, x2, y2, N);
  }
}
 
// This code is contributed by amreshkumar3.


Python3
# Python3 program for the above approach
 
# Function to find the maximum number
# of overlapping rectangles
def maxOverlappingRectangles(x1, y1, x2, y2, N):
 
    # Stores the maximum count of
    # overlapping rectangles
    max_rectangles = 0
 
    # Stores the X and Y coordinates
    X = []
    Y = []
 
    for i in range(0, N):
        X.append(x1[i])
        X.append(x2[i] - 1)
        Y.append(y1[i])
        Y.append(y2[i] - 1)
 
        # Iterate over all pairs of Xs and Ys
    for i in range(0, len(X)):
        for j in range(0, len(Y)):
 
           # Store the count for the
           # current X and Y
            cnt = 0
            for k in range(0, N):
                if (X[i] >= x1[k] and X[i] + 1 <= x2[k] and Y[j] >= y1[k] and Y[j] + 1 <= y2[k]):
                    cnt += 1
 
            # Update the maximum count of
            # rectangles
            max_rectangles = max(max_rectangles, cnt)
 
        # Returns the total count
    print(max_rectangles)
 
 
# Driver Code
if __name__ == "__main__":
 
    x1 = [0, 50]
    y1 = [0, 50]
    x2 = [100, 60]
    y2 = [100, 60]
    N = len(x1)
 
    maxOverlappingRectangles(x1, y1, x2, y2, N)
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
   
    // Function to find the maximum number
    // of overlapping rectangles
    static void maxOverlappingRectangles(int[] x1, int[] y1,
                                         int[] x2, int[] y2,
                                         int N)
    {
        // Stores the maximum count of
        // overlapping rectangles
        int max_rectangles = 0;
 
        // Stores the X and Y coordinates
        List X = new List();
        List Y = new List();
 
        for (int i = 0; i < N; i++) {
            X.Add(x1[i]);
            X.Add(x2[i] - 1);
            Y.Add(y1[i]);
            Y.Add(y2[i] - 1);
        }
 
        // Iterate over all pairs of Xs and Ys
        for (int i = 0; i < X.Count; i++) {
 
            for (int j = 0; j < Y.Count; j++) {
 
                // Store the count for the
                // current X and Y
                int cnt = 0;
                for (int k = 0; k < N; k++) {
 
                    if (X[i] >= x1[k] && X[i] + 1 <= x2[k]
                        && Y[j] >= y1[k]
                        && Y[j] + 1 <= y2[k]) {
                        cnt++;
                    }
                }
 
                // Update the maximum count of
                // rectangles
                max_rectangles
                    = Math.Max(max_rectangles, cnt);
            }
        }
 
        // Returns the total count
        Console.WriteLine(max_rectangles);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] x1 = { 0, 50 };
        int[] y1 = { 0, 50 };
        int[] x2 = { 100, 60 };
        int[] y2 = { 100, 60 };
        int N = x1.Length;
 
        maxOverlappingRectangles(x1, y1, x2, y2, N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2

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