📜  将一个盒子放进另一个盒子后可见盒子的数量

📅  最后修改于: 2021-04-23 06:10:53             🧑  作者: Mango

给定N个盒子,它们的大小成阵列。仅当其中容纳有盒子的盒子为空并且盒子的大小至少是盒子的大小的两倍时,才允许将盒子保留在另一个盒子中。任务是找到可见框的最小数量。

例子 –

Input : arr[] = { 1, 3, 4, 5 }
Output : 3
Put box of size 1 in box of size 3.

Input : arr[] = { 4, 2, 1, 8 }
Output : 1
Put box of size 1 in box of size 2 
and box of size 2 in box of size 4.
And put box of size 4 in box of size 8.

这个想法是对数组进行排序。现在,进行队列并插入已排序数组的第一个元素。现在从第一个元素开始遍历数组,并将每个元素插入队列中,还检查队列的前元素是否小于或等于当前被遍历元素的一半。因此,可见框的数量将是遍历已排序数组之后队列中元素的数量。基本上,我们试图将一个尺寸大于或等于2 * x的最小盒子放入一个盒子中。
例如,如果arr [] = {2,3,4,6},那么我们尝试将大小为2的框放入大小为4的框而不是大小为6的框,因为如果我们将大小为2的框放入大小为0的框6,则大小为3的框不能保留在任何其他框中,我们需要最小化可见框的数量。

C++
// CPP program to count number of visible boxes.
#include 
using namespace std;
  
// return the minimum number of visible boxes
int minimumBox(int arr[], int n)
{
    queue q;
  
    // sorting the array
    sort(arr, arr + n);
  
    q.push(arr[0]);
  
    // traversing the array
    for (int i = 1; i < n; i++)  {
  
        int now = q.front();
  
        // checking if current element
        // is greater than or equal to
        // twice of front element
        if (arr[i] >= 2 * now)
            q.pop();
  
        // Pushing each element of array
        q.push(arr[i]);
    }
  
    return q.size();
}
  
// driver Program
int main()
{
    int arr[] = { 4, 1, 2, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minimumBox(arr, n) << endl;
    return 0;
}


Java
// Java program to count number of visible
// boxes.
  
import java.util.LinkedList;
import java.util.Queue;
import java.util.Arrays;
  
public class GFG {
      
    // return the minimum number of visible
    // boxes
    static int minimumBox(int []arr, int n)
    {
          
        // New Queue of integers.
        Queue q = new LinkedList<>();
      
        // sorting the array
        Arrays.sort(arr);
      
        q.add(arr[0]);
          
        // traversing the array
        for (int i = 1; i < n; i++)
        {
            int now = q.element();
      
            // checking if current element
            // is greater than or equal to
            // twice of front element
            if (arr[i] >= 2 * now)
            q.remove();
      
            // Pushing each element of array
            q.add(arr[i]);
        }
      
        return q.size();
    } 
      
    // Driver code
    public static void main(String args[]) 
    {
        int [] arr = { 4, 1, 2, 8 };
        int n = arr.length;
          
        System.out.println(minimumBox(arr, n));
    }
}
  
// This code is contributed by Sam007.


Python3
# Python3 program to count number 
# of visible boxes.
  
import collections
  
# return the minimum number of visible boxes
def minimumBox(arr, n):
  
    q = collections.deque([])
  
    # sorting the array
    arr.sort()
  
    q.append(arr[0])
  
    # traversing the array
    for i in range(1, n):
  
        now = q[0]
  
        # checking if current element
        # is greater than or equal to
        # twice of front element
        if(arr[i] >= 2 * now):
            q.popleft()
  
        # Pushing each element of array
        q.append(arr[i])
  
    return len(q)
  
# driver Program
if __name__=='__main__':
    arr = [4, 1, 2, 8 ]
    n = len(arr)
    print(minimumBox(arr, n))
  
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to count number of visible
// boxes.
using System;
using System.Collections.Generic;
  
class GFG {
  
    // return the minimum number of visible
    // boxes
    static int minimumBox(int []arr, int n)
    {
          
        // New Queue of integers.
        Queue q = new Queue();
      
        // sorting the array
        Array.Sort(arr);
      
        q.Enqueue(arr[0]);
          
        // traversing the array
        for (int i = 1; i < n; i++)
        {
            int now = q.Peek();
      
            // checking if current element
            // is greater than or equal to
            // twice of front element
            if (arr[i] >= 2 * now)
            q.Dequeue();
      
            // Pushing each element of array
            q.Enqueue(arr[i]);
        }
      
        return q.Count;
    } 
      
    // Driver code
    public static void Main()
    {
        int [] arr = { 4, 1, 2, 8 };
        int n = arr.Length;
          
        Console.WriteLine(minimumBox(arr, n));
    }
}
  
// This code is contributed by Sam007.


PHP
= 2 * $now)
            array_pop($q); 
  
        // Pushing each element of array 
        array_push($q,$arr[$i]); 
    } 
  
    return count($q); 
} 
  
// Driver Code
$arr = array( 4, 1, 2, 8 ); 
$n = count($arr); 
echo minimumBox($arr, $n); 
  
// This code is contributed by mits 
?>


输出 –

1

时间复杂度: O(nlogn)