📌  相关文章
📜  可以放置在较大矩形内的较小矩形的数量

📅  最后修改于: 2021-04-22 04:16:02             🧑  作者: Mango

给定四个整数L,B,lb ,其中LB表示较大矩形的尺寸, lb表示较小矩形的尺寸,任务是计算可以在内部绘制的较小矩形的数量一个更大的矩形。
注意:较小的矩形可以部分重叠。

例子:

天真的方法:想法是遍历较大矩形的长度L和宽度B以计算可以在较大矩形范围内绘制的尺寸为lxb的较小矩形的数量。遍历后打印总数。

时间复杂度: O(L * B)
辅助空间: O(1)

高效的方法:上述问题可以使用置换和组合来解决。步骤如下:

  1. 使用长度L的较小矩形l的长度的总可能值由(L – 1 + 1)给出
  2. 使用长度B的较小矩形b的宽度的总可能值由(B – b + 1)给出
  3. 因此,可以形成的可能矩形的总数由下式给出:

下面是上述方法的实现:

C++
// C++ program for the above approach 
  
#include  
using namespace std; 
  
// Function to count smaller rectangles 
// within the larger rectangle 
int No_of_rectangles(int L, int B, 
                    int l, int b) 
{ 
    // If the dimension of the smaller 
    // rectangle is greater than the 
    // bigger one 
    if ((l > L) || (b > B)) { 
        return -1; 
    } 
  
    else { 
  
        // Return the number of smaller 
        // rectangles possible 
        return (L - l + 1) * (B - b + 1); 
    } 
} 
  
// Driver Code 
int main() 
{ 
    // Dimension of bigger rectangle 
    int L = 5, B = 3; 
  
    // Dimension of smaller rectangle 
    int l = 4, b = 1; 
  
    // Function call 
    cout << No_of_rectangles(L, B, l, b); 
  
    return 0; 
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to count smaller rectangles
// within the larger rectangle
static int No_of_rectangles(int L, int B,
                            int l, int b)
{
      
    // If the dimension of the smaller
    // rectangle is greater than the
    // bigger one
    if ((l > L) || (b > B)) 
    {
        return -1;
    }
  
    else
    {
          
        // Return the number of smaller
        // rectangles possible
        return (L - l + 1) * (B - b + 1);
    }
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Dimension of bigger rectangle
    int L = 5, B = 3;
  
    // Dimension of smaller rectangle
    int l = 4, b = 1;
  
    // Function call
    System.out.println(No_of_rectangles(L, B, l, b));
}
}
  
// This code is contributed by jana_sayantan


Python3
# Python3 program for the above approach
  
# Function to count smaller rectangles
# within the larger rectangle
def No_of_rectangles( L, B, l, b):
  
    # If the dimension of the smaller
    # rectangle is greater than the
    # bigger one
    if (l > L) or (b > B): 
        return -1;
          
    else:
  
        # Return the number of smaller
        # rectangles possible
        return (L - l + 1) * (B - b + 1);
      
# Driver code
if __name__ == '__main__': 
      
    # Dimension of bigger rectangle
    L = 5
    B = 3
  
    # Dimension of smaller rectangle
    l = 4
    b = 1
  
    # Function call
    print(No_of_rectangles(L, B, l, b))
  
# This code is contributed by jana_sayantan


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to count smaller rectangles
// within the larger rectangle
static int No_of_rectangles(int L, int B,
                            int l, int b)
{
      
    // If the dimension of the smaller
    // rectangle is greater than the
    // bigger one
    if ((l > L) || (b > B))
    {
        return -1;
    }
    else
    {
          
        // Return the number of smaller
        // rectangles possible
        return (L - l + 1) * (B - b + 1);
    }
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Dimension of bigger rectangle
    int L = 5, B = 3;
      
    // Dimension of smaller rectangle
    int l = 4, b = 1;
      
    // Function call
    Console.Write(No_of_rectangles(L, B, l, b));
}
}
  
// This code is contributed by jana_sayantan


输出:
6

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