📜  最大化保留至少一件黑色和一件白色衬衫所需的箱子

📅  最后修改于: 2021-09-16 10:52:32             🧑  作者: Mango

给定三个数字WBO 分别代表白色、黑色和其他颜色衬衫的数量,任务是找到所需的最大盒子数,使得每个盒子包含 三件衬衫,包括至少一件白色和黑色衬衫,使用给定数量的衬衫。

例子:

方法:给定的问题可以通过使用二分搜索来解决。思路是在由下界和上界形成的搜索空间中搜索最大数量的框。可以观察到box的count的下限和上限分别为0WB的最小值。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0来存储所需的结果。
  • 初始化两个变量,0(W, B)的最小值。
  • 循环直到low的值小于high并执行以下步骤:
    • 在变量中找到lowhigh的中间值,比如mid
    • 检查最大框数是否可以等于mid ,然后将ans的值更新为mid并通过更新low的值更新右半部分的搜索空间。
    • 否则,通过更新high的值将搜索空间更新到左半部分。
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum number
// of boxes such that each box contains
// three shirts comprising of at least
// one white and black shirt
void numberofBoxes(int W, int B, int O)
{
    // Stores the low and high pointers
    // for binary search
    int low = 0, high = min(W, B);
 
    // Store the required answer
    int ans = 0;
 
    // Loop while low <= high
    while (low <= high) {
 
        // Store the mid value
        int mid = low + (high - low) / 2;
 
        // Check if the mid number of
        // boxes can be used
        if (((W >= mid) and (B >= mid))
            and ((W - mid) + (B - mid) + O)
                    >= mid) {
 
            // Update answer and recur
            // for the right half
            ans = mid;
            low = mid + 1;
        }
 
        // Else, recur for the left half
        else
            high = mid - 1;
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    int W = 3, B = 3, O = 1;
    numberofBoxes(W, B, O);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum number
// of boxes such that each box contains
// three shirts comprising of at least
// one white and black shirt
static void numberofBoxes(int W, int B, int O)
{
     
    // Stores the low and high pointers
    // for binary search
    int low = 0, high = Math.min(W, B);
 
    // Store the required answer
    int ans = 0;
 
    // Loop while low <= high
    while (low <= high)
    {
         
        // Store the mid value
        int mid = low + (high - low) / 2;
 
        // Check if the mid number of
        // boxes can be used
        if (((W >= mid) && (B >= mid)) &&
            ((W - mid) + (B - mid) + O) >= mid)
        {
             
            // Update answer and recur
            // for the right half
            ans = mid;
            low = mid + 1;
        }
 
        // Else, recur for the left half
        else
            high = mid - 1;
    }
 
    // Print the result
    System.out.println(ans);
}
 
// Driver Code
public static void main(String args[])
{
    int W = 3, B = 3, O = 1;
     
    numberofBoxes(W, B, O);
}
}
 
// This code is contributed by avijitmondal1998


Python3
# Python3 program for the above approach
 
# Function to find the maximum number
# of boxes such that each box contains
# three shirts comprising of at least
# one white and black shirt
def numberofBoxes(W, B, O):
   
    # Stores the low and high pointers
    # for binary search
    low = 0
    high = min(W, B)
 
    # Store the required answer
    ans = 0
 
    # Loop while low <= high
    while (low <= high):
 
        # Store the mid value
        mid = low + (high - low) // 2
 
        # Check if the mid number of
        # boxes can be used
        if (((W >= mid) and (B >= mid)) and ((W - mid) + (B - mid) + O) >= mid):
            # Update answer and recur
            # for the right half
            ans = mid
            low = mid + 1
 
        # Else, recur for the left half
        else:
            high = mid - 1
 
    # Prthe result
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    W = 3
    B = 3
    O = 1
    numberofBoxes(W, B, O)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
 
using System;
class GFG {
 
    // Function to find the maximum number
    // of boxes such that each box contains
    // three shirts comprising of at least
    // one white and black shirt
    static void numberofBoxes(int W, int B, int O)
    {
        // Stores the low and high pointers
        // for binary search
        int low = 0, high = Math.Min(W, B);
 
        // Store the required answer
        int ans = 0;
 
        // Loop while low <= high
        while (low <= high) {
 
            // Store the mid value
            int mid = low + (high - low) / 2;
 
            // Check if the mid number of
            // boxes can be used
            if (((W >= mid) &&(B >= mid))
                    &&((W - mid) + (B - mid) + O)
                >= mid) {
 
                // Update answer and recur
                // for the right half
                ans = mid;
                low = mid + 1;
            }
 
            // Else, recur for the left half
            else
                high = mid - 1;
        }
 
        // Print the result
        Console.Write(ans);
    }
 
    // Driver Code
    public static void Main()
    {
        int W = 3, B = 3, O = 1;
        numberofBoxes(W, B, O);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2

时间复杂度: O(log(min(W, B)))
辅助空间: O(1)