📌  相关文章
📜  包含给定尺寸的N个非重叠矩形的最小正方形的大小

📅  最后修改于: 2021-04-17 17:56:40             🧑  作者: Mango

给定两个正整数WH以及N个尺寸为W * H的矩形,任务是找到所需最小的正方形尺寸,以便所有N个矩形都可以打包而不会重叠。

例子:

方法:给定的问题基于以下观察结果:

  • 可以证明,正方形内矩形的最佳间距之一由下式给出:

  • sidesX /W⌋⋅⌊X/H⌋给出了可与侧面X放置在正方形中的,尺寸为W * H的矩形的最大数量。
  • 以上函数是单调增加的。因此,其想法是使用二元搜索来找到满足给定条件的正方形的最小边。

请按照以下步骤解决问题:

  • 初始化两个变量,比如1W * H * N。
  • 迭代直到i小于j并执行以下步骤:
    • 找出mid的值为(i + j)/ 2
    • 现在,如果(mid / W)*(mid / H)的值最多为N ,则将high的值更新为mid
    • 否则,将low的值更新为(mid +1)
  • 完成上述步骤后,打印high值作为结果值。

下面是上述方法的实现:

Python3
# Python program for the above approach
 
# Function to check if side of square X
# can pack all the N rectangles or not
def bound(w, h, N, x):
 
    # Find the number of rectangle
    # it can pack
    val = (x//w)*(x//h)
     
    # If val is atleast N,
    # then return true
    if(val >= N):
        return True
       
    # Otherwise, return false
    else:
        return False
 
# Function to find the size of the
# smallest square that can contain
# N rectangles of dimensions W * H
def FindSquare(N, W, H):
 
    # Stores the lower bound
    i = 1
     
    # Stores the upper bound
    j = W * H*N
     
    # Iterate until i is less than j
    while(i < j):
       
        # Calculate the mid value
        mid = i + (j - i)//2
 
        # If the current size of square
        # cam contain N rectangles
        if(bound(W, H, N, mid)):
            j = mid
         
        # Otherwise, update i
        else:
            i = mid + 1
 
    # Return the minimum size of the
    # square required
    return j
 
# Driver Code
 
W = 2
H = 3
N = 10
 
# Function Call
print(FindSquare(N, W, H))


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if side of square X
// can pack all the N rectangles or not
static bool bound(int w, int h, int N, int x)
{
     
    // Find the number of rectangle
    // it can pack
    int val = (x / w) * (x / h);
 
    // If val is atleast N,
    // then return true
    if (val >= N)
        return true;
 
    // Otherwise, return false
    else
        return false;
}
 
// Function to find the size of the
// smallest square that can contain
// N rectangles of dimensions W * H
static int FindSquare(int N, int W, int H)
{
 
    // Stores the lower bound
    int i = 1;
 
    // Stores the upper bound
    int j = W * H * N;
 
    // Iterate until i is less than j
    while (i < j)
    {
         
        // Calculate the mid value
        int mid = i + (j - i) / 2;
 
        // If the current size of square
        // cam contain N rectangles
        if (bound(W, H, N, mid))
            j = mid;
 
        // Otherwise, update i
        else
            i = mid + 1;
    }
     
    // Return the minimum size of the
    // square required
    return j;
}
 
// Driver Code
public static void Main()
{
    int W = 2;
    int H = 3;
    int N = 10;
 
    // Function Call
    Console.WriteLine(FindSquare(N, W, H));
}
}
 
// This code is contributed by ukasp


输出:
9

时间复杂度: O(log(W * H))
辅助空间: O(1)