Minimum number of stacks possible using boxes of given capacities
Given N boxes with their capacities which denotes the total number of boxes that it can hold above it. You can stack up the boxes one over the other as long as the total number of boxes above each box is less than or equal to its capacity. Find the minimum number of stacks that can be made by using all the boxes.
Examples:
Input: arr[] = {0, 0, 1, 1, 2}
Output: 2
First stack (top to bottom): 0 1 2
Second stack (top to bottom): 0 1
Input: arr[] = {1, 1, 4, 4}
Output: 1
All the boxes can be put on a single stack.
Approach: Let’s have a map in which map[X] denotes the number of boxes with capacity X available with us. Let’s build stacks one by one. Initially the size of the stack would be 0, and then we iterate through the map greedily choosing as many boxes of current capacity as we can.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPiles( int n, int a[])
{
map< int , int > occ;
for ( int i = 0; i < n; i++)
occ[a[i]]++;
int pile = 0;
while (occ.size()) {
pile++;
int size = 0;
unordered_set< int > toRemove;
for ( auto tm : occ) {
int mx = tm .first;
int ct = tm .second;
int use = min(ct, mx - size + 1);
occ[mx] -= use;
size += use;
if (occ[mx] == 0)
toRemove.insert(mx);
}
for ( auto tm : toRemove)
occ.erase( tm );
}
return pile;
}
int main()
{
int a[] = { 0, 0, 1, 1, 2 };
int n = sizeof (a) / sizeof (a[0]);
cout << countPiles(n, a);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.HashSet;
class GFG
{
static int countPiles( int n, int [] a)
{
HashMap<Integer,
Integer> occ = new HashMap<>();
for ( int i = 0 ; i < n; i++)
occ.put(a[i], occ.get(a[i]) == null ? 1 :
occ.get(a[i]) + 1 );
int pile = 0 ;
while (!occ.isEmpty())
{
pile++;
int size = 0 ;
HashSet<Integer> toRemove = new HashSet<>();
for (HashMap.Entry<Integer,
Integer> tm : occ.entrySet())
{
int mx = tm.getKey();
int ct = tm.getValue();
int use = Math.min(ct, mx - size + 1 );
occ.put(mx, occ.get(mx) - use);
size += use;
if (occ.get(mx) == 0 )
toRemove.add(mx);
}
for ( int tm : toRemove)
occ.remove(tm);
}
return pile;
}
public static void main(String[] args)
{
int [] a = { 0 , 0 , 1 , 1 , 2 };
int n = a.length;
System.out.println(countPiles(n, a));
}
}
|
Python3
def countPiles(n, a):
occ = dict ()
for i in a:
if i in occ.keys():
occ[i] + = 1
else :
occ[i] = 1
pile = 0
while ( len (occ) > 0 ):
pile + = 1
size = 0
toRemove = dict ()
for tm in occ:
mx = tm
ct = occ[tm]
use = min (ct, mx - size + 1 )
occ[mx] - = use
size + = use
if (occ[mx] = = 0 ):
toRemove[mx] = 1
for tm in toRemove:
del occ[tm]
return pile
a = [ 0 , 0 , 1 , 1 , 2 ]
n = len (a)
print (countPiles(n, a))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int countPiles( int n, int [] a)
{
Dictionary< int ,
int > occ = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (!occ.ContainsKey(a[i]))
{
occ[a[i]]=0;
}
occ[a[i]]++;
}
int pile = 0;
while (occ.Count!=0)
{
pile++;
int size = 0;
HashSet< int > toRemove = new HashSet< int >();
Dictionary< int , int > tmp = occ;
foreach ( var tm in occ.Keys.ToList())
{
int mx = tm;
int ct = occ[tm];
int use = Math.Min(ct, mx - size + 1);
occ[mx]-= use;
size += use;
if (occ[mx] == 0)
toRemove.Add(mx);
}
occ = tmp;
foreach ( int tm in toRemove.ToList())
occ.Remove(tm);
}
return pile;
}
public static void Main( string [] args)
{
int [] a = { 0, 0, 1, 1, 2 };
int n = a.Length;
Console.WriteLine(countPiles(n, a));
}
}
|
Javascript
<script>
function countPiles(n, a)
{
let occ = new Map();
for (let i = 0; i < n; i++)
occ.set(a[i], occ.get(a[i]) == null ? 1 :
occ.get(a[i]) + 1);
let pile = 0;
while (occ.size != 0)
{
pile++;
let size = 0;
let toRemove = new Set();
for (let [key, value] of occ.entries())
{
let mx = key;
let ct = value;
let use = Math.min(ct, mx - size + 1);
occ.set(mx, occ.get(mx) - use);
size += use;
if (occ.get(mx) == 0)
toRemove.add(mx);
}
for (let tm of toRemove.values())
occ. delete (tm);
}
return pile;
}
let a = [ 0, 0, 1, 1, 2 ];
let n = a.length;
document.write(countPiles(n, a));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N), where N is the size of the given input array.
Last Updated :
15 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...