Problem Statement
You are buying a product that comes in packages of varying sizes, given in
Return the minimum number of packages you must buy in order to purchase exactly total units. If it is not possible to do so, return -1.
Definition
- Class:
- PackageSizes
- Method:
- getMinimum
- Parameters:
- int[], int
- Returns:
- int
- Method signature:
- int getMinimum(int[] sizes, int total)
- (be sure your method is public)
Constraints
- sizes will contain between 1 and 10 elements, inclusive.
- Each element of sizes will be between 1 and 100, inclusive.
- total will be between 0 and 1000, inclusive.
Examples
{ 2, 3 }
6
Returns: 2
We could buy 3 packages of 2, or 2 packages of 3. 2 packages is the least we need.
{ 6, 9, 20 }
8
Returns: -1
There's no way to buy exactly 8 with these package sizes.
{ 2, 5, 12 }
7
Returns: 2
We need a package of 5 and a package of 2.
{ 2, 5, 12 }
0
Returns: 0
We don't have to buy any packages at all.
{ 13, 50, 99 }
894
Returns: 12
{ 3, 2, 7, 3 }
10
Returns: 2
Package sizes aren't necessarily listed in order, and can be repeated.
{1, 5, 12 }
15
Returns: 3
{2, 3 }
5
Returns: 2
{10 }
10
Returns: 1
{10 }
1000
Returns: 100
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
1000
Returns: 1000
{2, 3, 5 }
6
Returns: 2
{1, 2, 8 }
11
Returns: 3
{2, 3, 5, 7 }
11
Returns: 3
{15, 7, 2 }
53
Returns: 6
{3, 5 }
11
Returns: 3
{2, 3 }
6
Returns: 2