📜  给定坐标下最大矩形的面积

📅  最后修改于: 2021-05-19 17:59:23             🧑  作者: Mango

给定两个阵列ARR1 []以及N表示的形式的N个坐标(ARR1 [I],0)M的正整数,表示的形式的M个坐标(ARR2 [J],1),其中1≤I≤ÑARR2 []1≤j≤M 。任务是找到可以使用这些坐标形成的最大矩形区域。如果无法形成矩形,则打印0

例子:

天真的方法:最简单的方法是遍历给定的数组arr1 [] ,对于每个i ,使用变量j遍历arr2 []中的点。如果arr1 [i]等于arr2 [j] ,则将值arr1 [i]存储在单独的数组中,例如ans [] 。找到所有这些值之后,对ans []数组进行排序并将最大面积打印为ans [L] – ans [0] ,其中Lans []的最后一个元素的索引,因为矩形的宽度将始终为1

时间复杂度: O(N * M)
辅助空间: O(M + N)

高效的方法:想法是使用排序算法和两指针技术。请注意,矩形的宽度将始终为1 。因此,可以通过最大长度来找到最大面积。请按照以下步骤解决问题:

  • 对给定数组arr1 []arr2 []进行排序。
  • 初始化变量,开始结束0来存储长度的开始和结束点。另外,初始化变量ij分别遍历数组arr1 []arr2 []
  • i小于Nj小于M时,请检查以下条件:
    • 如果arr1 [i]arr2 [j]相同且start为0 ,则将start更新为start = arr1 [i] 。否则,更新结束作为端= ARR1 [I]然后通过递增1 ij。
    • 如果ARR1 [i]是比ARR2 [j]时,通过1个增量Ĵ更大。否则,将i增加1
  • 如果开始结束0 ,则打印0 。否则,请打印(结束–开始)作为最大可能区域。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum possible
// area of a rectangle
int largestArea(int arr1[], int n,
                int arr2[], int m)
{
    // Initialize variables
    int end = 0, start = 0, i = 0, j = 0;
 
    // Sort array arr1[]
    sort(arr1, arr1 + n);
 
    // Sort array arr2[]
    sort(arr2, arr2 + m);
 
    // Traverse arr1[] and arr2[]
    while (i < n and j < m) {
 
        // If arr1[i] is same as arr2[j]
        if (arr1[i] == arr2[j]) {
 
            // If no starting point
            // is found yet
            if (start == 0)
                start = arr1[i];
            else
                // Update maximum end
                end = arr1[i];
            i++;
            j++;
        }
 
        // If arr[i] > arr2[j]
        else if (arr1[i] > arr2[j])
            j++;
        else
            i++;
    }
 
    // If no rectangle is found
    if (end == 0 or start == 0)
        return 0;
    else
        // Return the area
        return (end - start);
}
 
// Driver Code
int main()
{
    // Given point
    int arr1[] = { 1, 2, 4 };
 
    // Given length
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Given points
    int arr2[] = { 1, 3, 4 };
 
    // Given length
    int M = sizeof(arr2) / sizeof(arr2[0]);
 
    // Function Call
    cout << largestArea(arr1, N, arr2, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum possible
// area of a rectangle
static int largestArea(int arr1[], int n,
                       int arr2[], int m)
{
     
    // Initialize variables
    int end = 0, start = 0, i = 0, j = 0;
 
    // Sort array arr1[]
    Arrays.sort(arr1);
 
    // Sort array arr2[]
    Arrays.sort(arr1);
 
    // Traverse arr1[] and arr2[]
    while (i < n && j < m)
    {
         
        // If arr1[i] is same as arr2[j]
        if (arr1[i] == arr2[j])
        {
             
            // If no starting point
            // is found yet
            if (start == 0)
                start = arr1[i];
            else
             
                // Update maximum end
                end = arr1[i];
                 
            i++;
            j++;
        }
 
        // If arr[i] > arr2[j]
        else if (arr1[i] > arr2[j])
            j++;
        else
            i++;
    }
 
    // If no rectangle is found
    if (end == 0 || start == 0)
        return 0;
    else
     
        // Return the area
        return (end - start);
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given point
    int arr1[] = { 1, 2, 4 };
 
    // Given length
    int N = arr1.length;
 
    // Given points
    int arr2[] = { 1, 3, 4 };
 
    // Given length
    int M = arr2.length;
 
    // Function Call
    System.out.println(largestArea(arr1, N,
                                   arr2, M));
}
}
   
// This code is contributed by bolliranadheer


Python3
# Python3 program for the above approach
 
# Function to find the maximum possible
# area of a rectangle
def largestArea(arr1, n, arr2, m):
     
    # Initialize variables
    end = 0
    start = 0
    i = 0
    j = 0
 
    # Sort array arr1[]
    arr1.sort(reverse = False)
 
    # Sort array arr2[]
    arr2.sort(reverse = False)
 
    # Traverse arr1[] and arr2[]
    while (i < n and j < m):
         
        # If arr1[i] is same as arr2[j]
        if (arr1[i] == arr2[j]):
             
            # If no starting point
            # is found yet
            if (start == 0):
                start = arr1[i]
            else:
                 
                # Update maximum end
                end = arr1[i]
                 
            i += 1
            j += 1
 
        # If arr[i] > arr2[j]
        elif (arr1[i] > arr2[j]):
            j += 1
        else:
            i += 1
 
    # If no rectangle is found
    if (end == 0 or start == 0):
        return 0
    else:
         
        # Return the area
        return (end - start)
 
# Driver Code
if __name__ == '__main__':
     
    # Given point
    arr1 = [ 1, 2, 4 ]
 
    # Given length
    N = len(arr1)
 
    # Given points
    arr2 = [ 1, 3, 4 ]
 
    # Given length
    M =  len(arr2)
 
    # Function Call
    print(largestArea(arr1, N, arr2, M))
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum possible
// area of a rectangle
static int largestArea(int[] arr1, int n,
                       int[] arr2, int m)
{
     
    // Initialize variables
    int end = 0, start = 0, i = 0, j = 0;
  
    // Sort array arr1[]
    Array.Sort(arr1);
     
    // Sort array arr2[]
    Array.Sort(arr2);
  
    // Traverse arr1[] and arr2[]
    while (i < n && j < m)
    {
         
        // If arr1[i] is same as arr2[j]
        if (arr1[i] == arr2[j])
        {
             
            // If no starting point
            // is found yet
            if (start == 0)
                start = arr1[i];
            else
             
                // Update maximum end
                end = arr1[i];
                  
            i++;
            j++;
        }
  
        // If arr[i] > arr2[j]
        else if (arr1[i] > arr2[j])
            j++;
        else
            i++;
    }
  
    // If no rectangle is found
    if (end == 0 || start == 0)
        return 0;
    else
      
        // Return the area
        return (end - start);
}
 
// Driver code
static void Main()
{
     
    // Given point
    int[] arr1 = { 1, 2, 4 };
  
    // Given length
    int N = arr1.Length;
  
    // Given points
    int[] arr2 = { 1, 3, 4 };
  
    // Given length
    int M = arr2.Length;
  
    // Function Call
    Console.WriteLine(largestArea(arr1, N,
                                  arr2, M));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
3

时间复杂度: O(N * log N + M * log M)
辅助空间: O(M + N)