📜  从给定坐标可能的最大矩形的面积

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

给定两个数组arr1[]arr2[]N表示形式(arr1[i], 0) 的N 个坐标和表示形式(arr2[j], 1 ) 的M 个坐标的M 个正整数其中1 ≤ i ≤ N并且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
  • 如果startend0 ,则打印0 。否则,打印(end – start)作为最大可能区域。

下面是上述方法的实现:

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)

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