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++
#include <bits/stdc++.h>
using namespace std;
void bestFit( int arr[], int n, int W)
{
int count = 0;
sort(arr, arr + n, greater< int >());
multiset< int > M;
for ( int i = 0; i < n; i++) {
auto x = M.find(arr[i]);
auto y = M.upper_bound(arr[i]);
if (x != M.end()) {
M.erase(x);
}
else if (y != M.end()) {
M.insert(*y - arr[i]);
M.erase(y);
}
else {
count++;
M.insert(W - arr[i]);
}
}
cout << count;
}
int main()
{
int items[] = { 4, 8, 1, 4, 2, 1 };
int W = 10;
int N = sizeof (items) / sizeof (items[0]);
bestFit(items, N, W);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static void bestFit( int [] arr, int n, int W)
{
int count = 0 ;
Arrays.sort(arr);
for ( int i= 0 ;i<arr.length/ 2 ;i++){
int temp = arr[i];
arr[i] = arr[arr.length-i- 1 ];
arr[arr.length-i- 1 ] = temp;
}
TreeSet<Integer> M = new TreeSet<>();
for ( int i = 0 ; i < n; i++) {
Integer x = M.floor(arr[i]);
Integer y = M.higher(arr[i]);
if (x != null ) {
M.remove(x);
}
else if (y != null ) {
M.add(y - arr[i]);
M.remove(y);
}
else {
count++;
M.add(W - arr[i]);
}
}
System.out.println(count);
}
public static void main(String[] args) {
int [] items = { 4 , 8 , 1 , 4 , 2 , 1 };
int W = 10 ;
int N = items.length;
bestFit(items, N, W);
}
}
|
Python3
from typing import List
from collections import defaultdict
def bestFit(arr: List [ int ], n: int , W: int ) - > None :
count = 0
arr.sort(reverse = True )
M = defaultdict( int )
for i in range (n):
if arr[i] in M:
del M[arr[i]]
elif arr[i] + 1 in M:
M[M[arr[i] + 1 ] - arr[i]] = M[arr[i] + 1 ]
del M[arr[i] + 1 ]
else :
count + = 1
M[W - arr[i]] = W
print (count)
items = [ 4 , 8 , 1 , 4 , 2 , 1 ]
W = 10
N = len (items)
bestFit(items, N, W)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG
{
public static void bestFit( int [] arr, int n, int W)
{
int count = 0;
Array.Sort(arr);
arr = arr.Reverse().ToArray();
SortedSet< int > M = new SortedSet< int >();
for ( int i = 0; i < n; i++)
{
int x = M.GetViewBetween( int .MinValue, arr[i]).LastOrDefault();
int y = M.GetViewBetween(arr[i], int .MaxValue).FirstOrDefault();
if (x != 0)
{
M.Remove(x);
}
else if (y != 0)
{
M.Add(y - arr[i]);
M.Remove(y);
}
else
{
count++;
M.Add(W - arr[i]);
}
}
Console.WriteLine(count);
}
public static void Main( string [] args)
{
int [] items = { 4, 8, 1, 4, 2, 1 };
int W = 10;
int N = items.Length;
bestFit(items, N, W);
}
}
|
Javascript
function bestFit(arr, n, W) {
let count = -1;
arr.sort((a, b) => b - a);
let M = new Set();
for (let i = 0; i < n; i++) {
if (M.has(arr[i])) {
M. delete (arr[i]);
} else {
let y = Array.from(M).find(x => x > arr[i]);
if (y) {
M.add(y - arr[i]);
M. delete (y);
}
else {
count++;
M.add(W - arr[i]);
}
}
}
console.log(count);
}
let items = [4, 8, 1, 4, 2, 1];
let W = 10;
let N = items.length;
bestFit(items, N, W);
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Jan, 2023
Like Article
Save Article