给定四个整数L、B、l和b ,其中L和B表示较大矩形的尺寸, l和b表示较小矩形的尺寸,任务是计算可以在内部绘制的较小矩形的数量一个更大的矩形。
注意:较小的矩形可以部分重叠。
例子:
Input: L = 5, B = 3, l = 4, b = 1
Output: 6
Explanation:
There are 6 rectangles of dimension 4 × 1 that can be drawn inside a bigger rectangle of dimension 5 × 3.
Input: L = 3, B = 2, l = 2, b = 1
Output: 3
Explanation:
There are 3 rectangles of dimension 3 × 2 can be drawn inside a bigger rectangle of dimension 2 × 1.
朴素的方法:想法是迭代较大矩形的长度L和宽度B ,以计算在较大矩形范围内可以绘制的尺寸为lxb的较小矩形的数量。遍历后打印总计数。
时间复杂度: O(L * B)
辅助空间: O(1)
高效方法:上述问题可以使用排列组合来解决。以下是步骤:
- 使用长度L的较小矩形l的总可能值由(L – l + 1) 给出。
- 使用长度B的较小矩形b的宽度的总可能值由(B – b + 1) 给出。
- 因此,可以形成的可能矩形的总数由下式给出:
(L – l + 1) * (B – b + 1)
下面是上述方法的实现:
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
Javascript
输出:
6
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。