📌  相关文章
📜  没有给定点的最大矩形的面积

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

给定矩形的长L和宽B以及矩形中孔的位置作为(X, Y)坐标,任务是在给定的 Rectangle 中找到最大的 Rectangle 区域,使其不包含孔。
注意:矩形放置在原点处,其边的两个接触坐标轴。
例子:

处理方法:为了避开孔洞,可以从孔洞的上方、下方、左侧或右侧切割矩形,如下所示:

Position - Maximum area of rectangle
------------------------------------
Left     - X * B
Right    - (L - X - 1) * B
Above    - L * Y
Below    - (B - Y - 1) * L

因此,可以通过比较使用上述位置计算的面积来计算最大矩形的所需面积。面积最大的位置将产生结果。
下面是上述方法的实现:

C++
// C++ implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
 
#include 
using namespace std;
 
// Function to find the maximum area
// such that it does not contains any hole
void maximumArea(int l, int b,
                 int x, int y)
{
 
    // Area for all the possible
    // positions of the cut
    int left, right, above, below;
 
    left = x * b;
    right = (l - x - 1) * b;
    above = l * y;
    below = (b - y - 1) * l;
 
    // Find the maximum area
    // among the above rectangles
    cout << max(max(left, right),
                max(above, below));
}
 
// Driver Code
int main()
{
    int L = 8, B = 8;
    int X = 0, Y = 0;
 
    // Function call
    maximumArea(l, b, x, y);
 
    return 0;
}


Java
// Java implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
import java.util.*;
 
class GFG{
  
// Function to find the maximum area
// such that it does not contains any hole
static void maximumArea(int l, int b,
                 int x, int y)
{
  
    // Area for all the possible
    // positions of the cut
    int left, right, above, below;
  
    left = x * b;
    right = (l - x - 1) * b;
    above = l * y;
    below = (b - y - 1) * l;
  
    // Find the maximum area
    // among the above rectangles
    System.out.print(Math.max(Math.max(left, right),
                Math.max(above, below)));
}
  
// Driver Code
public static void main(String[] args)
{
    int L = 8, B = 8;
    int X = 0, Y = 0;
  
    // Function call
    maximumArea(L, B, X, Y);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation to find area of
# largest Rectangle without hole
# within a given Rectangle
 
# Function to find the maximum area
# such that it does not contains any hole
def maximumArea(l, b,x, y):
 
    # Area for all the possible
    # positions of the cut
    left, right, above, below = 0, 0, 0, 0
 
    left = x * b
    right = (l - x - 1) * b
    above = l * y
    below = (b - y - 1) * l
 
    # Find the maximum area
    # among the above rectangles
    print(max(max(left, right),max(above, below)))
 
# Driver Code
l = 8
b = 8
x = 0
y = 0
 
# Function call
maximumArea(l, b, x, y)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
using System;
 
class GFG{
   
// Function to find the maximum area
// such that it does not contains any hole
static void maximumArea(int l, int b,
                 int x, int y)
{
   
    // Area for all the possible
    // positions of the cut
    int left, right, above, below;
   
    left = x * b;
    right = (l - x - 1) * b;
    above = l * y;
    below = (b - y - 1) * l;
   
    // Find the maximum area
    // among the above rectangles
    Console.Write(Math.Max(Math.Max(left, right),
                Math.Max(above, below)));
}
   
// Driver Code
public static void Main(String[] args)
{
    int L = 8, B = 8;
    int X = 0, Y = 0;
   
    // Function call
    maximumArea(L, B, X, Y);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
56

性能分析:

  • 时间复杂度:有一个简单的计算,不涉及任何迭代或递归。因此,时间复杂度将为O(1)
  • 辅助空间复杂度:没有使用额外的空间。因此,辅助空间复杂度将为O(1)

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