Shopping
SRM 331 · 2006-12-21 · by slex
Problem Statement
Christmas is coming soon and you still have so many things to buy. You are going to the store and don't expect to spend more than X dollars. You want to be able to pay any integer amount not exceeding X dollars, and you want to take as few coins as possible to achieve this.
You are given a
Constraints
- X will be between 1 and 1000, inclusive.
- values will contain between 1 and 10 elements, inclusive.
- Each element of values will be between 1 and 1000, inclusive.
- The elements in values will be distinct.
5
{1}
Returns: 5
1000
{1}
Returns: 1000
1000
{1, 2}
Returns: 501
5
{1, 2}
Returns: 3
3
{1, 2}
Returns: 2
20
{1, 2, 5, 10}
Returns: 5
Taking 5 coins with values {1,2,2,5,10} allows you to pay any integer amount between 1 and 20, inclusive.
7
{2, 4, 1, 7}
Returns: 3
Here, taking {2,4,1} is enough.
20
{2,4,6,8}
Returns: -1
These nominals allow you to pay only even amounts.
Submissions are judged against all 75 archived test cases, of which 8 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Shopping with a public method int minNumber(int X, vector<int> values) · 75 test cases · 2 s / 256 MB per case