PackageSizes
Rookie SRM 7 · 2021-11-25 · by erinn
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.
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.
{ 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.
Submissions are judged against all 17 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PackageSizes with a public method int getMinimum(vector<int> sizes, int total) · 17 test cases · 2 s / 256 MB per case