-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimize_bin.cpp
52 lines (46 loc) · 1.14 KB
/
minimize_bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
using namespace std;
// Returns number of bins required using next fit
// online algorithm
int firstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i=0; i<n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j=0; j<res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j==res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver program
int main()
{
int weight[] = {6,3,7,4};
int c = 10;
sort(weight,weight+sizeof(weight)/sizeof(int));
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in First Fit : "
<< firstFit(weight, n, c)<<endl;;
return 0;
}