Skip to content
Related Articles

Related Articles

Minimum number of bins required to place N items ( Using Best Fit algorithm )
  • Last Updated : 09 Apr, 2021

Given an array weight[] consisting of weights of N items and a positive integer C representing the capacity of each bin, the task is to find the minimum number of bins required such that all items are assigned to one of the bins.

Examples:

Input: weight[] = {4, 8, 1, 4, 2, 1}, C = 10
Output: 2
Explanation: The minimum number of bins required to accommodate all items is 2.
The first bin contains the items with weights {4, 4, 2}.
The second bin contains the items with weights {8, 1, 1}.

Input: weight[] = {9, 8, 2, 2, 5, 4}, C = 10
Output: 4

Approach: The given problem can be solved by using the best-fit algorithm. The idea is to place the next item in the bin, where the smallest empty space is left. Follow the steps below to solve the problem:



  • Initialize a variable, say count as 0 that stores the minimum number of bins required.
  • Sort the given array weight[] in decreasing order.
  • Initialize a multiset, say M to store the empty spaces left in the occupied bins presently.
  • Traverse the array weight[] and for each element perform the following steps:
    • If there exists the smallest empty space which is at least arr[i] is present in the M, then erase that space from M and insert the remaining free space to M.
    • Otherwise, increment the count by 1 and insert the empty space of the new bin in M.
  • After completing the above steps, print the value of count as the minimum number of bins required.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum number
// of bins required to fill all items
void bestFit(int arr[], int n, int W)
{
    // Stores the required number
    // of bins
    int count = 0;
  
    // Sort the array in decreasing order
    sort(arr, arr + n, greater<int>());
  
    // Stores the empty spaces in
    // existing bins
    multiset<int> M;
  
    // Traverse the given array
    for (int i = 0; i < n; i++) {
  
        // Check if exact space is
        // present in the set M
        auto x = M.find(arr[i]);
  
        // Store the position of the
        // upperbound of arr[i] in M
        auto y = M.upper_bound(arr[i]);
  
        // If arr[i] is present, then
        // use this space and erase it
        // from the map M
        if (x != M.end()) {
            M.erase(x);
        }
  
        // If upper bound of arr[i] is
        // present, use this space and
        // insert the left space
        else if (y != M.end()) {
            M.insert(*y - arr[i]);
            M.erase(y);
        }
  
        // Otherwise, increment the count
        // of bins and insert the
        // empty space in M
        else {
            count++;
            M.insert(W - arr[i]);
        }
    }
  
    // Print the result
    cout << count;
}
  
// Driver Code
int main()
{
    int items[] = { 4, 8, 1, 4, 2, 1 };
    int W = 10;
    int N = sizeof(items) / sizeof(items[0]);
  
    // Function Call
    bestFit(items, N, W);
  
    return 0;
}
Output:
2

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :