Skip to main content

← Dynamic programming

Fewest coins

medium

The first line has the amount and the number of coin types. The second line has the coin values. Print the fewest coins that make the amount exactly, or -1 if it can't be done. Each coin can be used any number of times.

Example 1: input

11 3
1 2 5

Output

3

Example 2: input

3 1
2

Output

-1

Plus 3 hidden tests when you check your answer.

Your solution

Read from standard input and print the answer. Run it on the examples as often as you like, then check it against every test.

Stuck?

Open hints one at a time. The write-up explains a full solution.

Discussion

Ask a question or share a tip. Please don’t post full solutions. Posts appear once a moderator approves them.

No posts yet.