-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitcase.java
72 lines (58 loc) · 1.87 KB
/
fitcase.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package OS;
import java.util.ArrayList;
import java.util.List;
class FirstFit {
private static class Bin {
int id;
int capacity;
int occupied;
Bin(int id, int capacity) {
this.id = id;
this.capacity = capacity;
this.occupied = 0;
}
}
private static class Item {
int id;
int size;
Item(int id, int size) {
this.id = id;
this.size = size;
}
}
private static List<Bin> firstFit(List<Item> items, int binCapacity) {
List<Bin> bins = new ArrayList<>();
for (Item item : items) {
boolean itemPlaced = false;
// Try to fit the item in an existing bin
for (Bin bin : bins) {
if (bin.capacity - bin.occupied >= item.size) {
bin.occupied += item.size;
itemPlaced = true;
break;
}
}
// If the item couldn't be placed, create a new bin and put the item in it
if (!itemPlaced) {
Bin newBin = new Bin(bins.size() + 1, binCapacity);
newBin.occupied = item.size;
bins.add(newBin);
}
}
return bins;
}
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item(1, 4));
items.add(new Item(2, 2));
items.add(new Item(3, 5));
items.add(new Item(4, 1));
items.add(new Item(5, 3));
int binCapacity = 10;
List<Bin> bins = firstFit(items, binCapacity);
// Print the result
for (Bin bin : bins) {
System.out.println("Bin " + bin.id + ": Occupied " + bin.occupied + "/" + bin.capacity);
}
}
}