📌  相关文章
📜  通过将小盒子放在大盒子中来最大程度地减少盒子数量

📅  最后修改于: 2021-04-23 05:44:13             🧑  作者: Mango

给定一个大小为box []的数组size [] ,我们的任务是在将较小大小的盒子放入较大的盒子之后,找到最后剩下的盒子数量。
注意:一个盒子里只能装一个小盒子。
例子:

方法:想法是遵循以下步骤:

  • 以递增的顺序对给定的数组size []进行排序,并检查当前框的大小是否大于下一个框的大小。如果是,则减少初始框号。
  • 否则,如果当前框的大小等于下一个框的大小,请检查当前框是否适合下一个框的大小。如果是,则移动当前的框指向变量,否则进一步移动下一个指向变量。

下面是上述方法的实现:

C++
// C++ implementation to minimize the
// number of the box by putting small
// box inside the bigger one
 
#include 
using namespace std;
 
// Function to minimize the count
void minBox(int arr[], int n)
{
    // Initial number of box
    int box = n;
 
    // Sort array of box size
    // in increasing order
    sort(arr, arr + n);
 
    int curr_box = 0, next_box = 1;
    while (curr_box < n && next_box < n) {
 
        // check is current box size
        // is smaller than next box size
        if (arr[curr_box] < arr[next_box]) {
 
            // Decrement box count
            // Increment current box count
            // Increment next box count
            box--;
            curr_box++;
            next_box++;
        }
 
        // Check if both box
        // have same size
        else if (arr[curr_box] == arr[next_box])
            next_box++;
    }
 
    // Print the result
    cout << box << endl;
}
 
// Driver code
int main()
{
    int size[] = { 1, 2, 3 };
    int n = sizeof(size) / sizeof(size[0]);
    minBox(size, n);
    return 0;
}


Java
// Java implementation to minimize the
// number of the box by putting small
// box inside the bigger one
import java.util.Arrays;
 
class GFG{
     
// Function to minimize the count
public static void minBox(int arr[], int n)
{
     
    // Initial number of box
    int box = n;
 
    // Sort array of box size
    // in increasing order
    Arrays.sort(arr);
 
    int curr_box = 0, next_box = 1;
    while (curr_box < n && next_box < n)
    {
         
        // Check is current box size
        // is smaller than next box size
        if (arr[curr_box] < arr[next_box])
        {
             
            // Decrement box count
            // Increment current box count
            // Increment next box count
            box--;
            curr_box++;
            next_box++;
        }
 
        // Check if both box
        // have same size
        else if (arr[curr_box] ==
                 arr[next_box])
            next_box++;
    }
 
    // Print the result
    System.out.println(box);
}
 
// Driver code
public static void main(String args[])
{
    int []size = { 1, 2, 3 };
    int n = size.length;
     
    minBox(size, n);
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 implementation to minimize the
# number of the box by putting small
# box inside the bigger one
 
# Function to minimize the count
def minBox(arr, n):
     
    # Initial number of box
    box = n
 
    # Sort array of box size
    # in increasing order
    arr.sort()
 
    curr_box, next_box = 0, 1
    while (curr_box < n and next_box < n):
 
        # Check is current box size
        # is smaller than next box size
        if (arr[curr_box] < arr[next_box]):
 
            # Decrement box count
            # Increment current box count
            # Increment next box count
            box = box - 1
            curr_box = curr_box + 1
            next_box = next_box + 1
 
        # Check if both box
        # have same size
        elif (arr[curr_box] == arr[next_box]):
            next_box = next_box + 1
 
    # Print the result
    print(box)
 
# Driver code
size = [ 1, 2, 3 ]
n = len(size)
 
minBox(size, n)
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation to minimize the
// number of the box by putting small
// box inside the bigger one
using System;
 
class GFG{
     
// Function to minimize the count
public static void minBox(int []arr, int n)
{
     
    // Initial number of box
    int box = n;
 
    // Sort array of box size
    // in increasing order
    Array.Sort(arr);
 
    int curr_box = 0, next_box = 1;
    while (curr_box < n && next_box < n)
    {
         
        // Check is current box size
        // is smaller than next box size
        if (arr[curr_box] < arr[next_box])
        {
             
            // Decrement box count
            // Increment current box count
            // Increment next box count
            box--;
            curr_box++;
            next_box++;
        }
 
        // Check if both box
        // have same size
        else if (arr[curr_box] ==
                 arr[next_box])
            next_box++;
    }
 
    // Print the result
    Console.WriteLine(box);
}
 
// Driver code
public static void Main(String []args)
{
    int []size = { 1, 2, 3 };
    int n = size.Length;
     
    minBox(size, n);
}
}
 
// This code is contributed by Amit Katiyar


Java
// Java implementation of the
// above approach
 
import java.util.*;
 
public class Boxes {
   
    // Function to count the boxes
    // at the end
    int countBoxes(int[] A) {
       
        // Default value if
        // array is empty
        int count = -1;
        int n = A.length;
       
         // Map to store frequencies
        Map map =
          new HashMap<>();
       
        // Loop to iterate over the
        // elements of the array
        for (int i=0; i count) count = val;
        }
        return count;
    }
     
    // Driver Code
    public static void main(
                  String[] args)
    {
        int[] a = {8, 15, 1, 10, 5, 1};
        Boxes obj = new Boxes();
       
        // Function Call
        int minBoxes = obj.countBoxes(a);
          System.out.println(minBoxes);
    }
}


输出
1

时间复杂度:使用Arrays.sort()方法时为O(N * logN)。
辅助空间: O(1)

方法:该问题的主要观察结果是,最少数量的盒子与数组中任何元素的最大频率相同。因为其余元素相互调整。下面是在步骤帮助下的示意图:

  • 创建一个哈希图以存储元素的频率。
  • 最后,在保持频率后,返回元件的最大频率。

下面是上述方法的实现:

Java

// Java implementation of the
// above approach
 
import java.util.*;
 
public class Boxes {
   
    // Function to count the boxes
    // at the end
    int countBoxes(int[] A) {
       
        // Default value if
        // array is empty
        int count = -1;
        int n = A.length;
       
         // Map to store frequencies
        Map map =
          new HashMap<>();
       
        // Loop to iterate over the
        // elements of the array
        for (int i=0; i count) count = val;
        }
        return count;
    }
     
    // Driver Code
    public static void main(
                  String[] args)
    {
        int[] a = {8, 15, 1, 10, 5, 1};
        Boxes obj = new Boxes();
       
        // Function Call
        int minBoxes = obj.countBoxes(a);
          System.out.println(minBoxes);
    }
}
输出
2

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