-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlourPacker.java
More file actions
36 lines (31 loc) · 1.22 KB
/
Copy pathFlourPacker.java
File metadata and controls
36 lines (31 loc) · 1.22 KB
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
public class FlourPacker {
public static void main(String[] args) {
System.out.println(canPack(2, 10, 18));
}
public static boolean canPack(int bigBags, int smallBags, int goal) {
if (bigBags < 0 || smallBags < 0 || goal < 0) {
return false;
}
int bigBagSum = bigBags * 5;
if (bigBagSum + smallBags >= goal) {
// Enough bags exist, but can they be evenly divided?
if ((bigBagSum + smallBags) % goal == 0) {
// Exactly enough bags exist
return true;
} else if ((goal - bigBagSum) <= 0 && (goal % 5) <= smallBags) {
// If we have more then enough big bags that are required and a small amount of
// small bags to cover the differnce (I.E: (6,2,17)
return true;
} else if ((goal - bigBagSum) > 0 && (goal - bigBagSum) - smallBags <= 0) {
// If we dont have enough big bags, but we have plenty of small ones
return true;
} else {
// Can't divide bags evenly
return false;
}
} else {
// Not enough bags exist
return false;
}
}
}