Fewest coins
Level B. Teaches a DP over ?Int with min=.
There are n coin values (1 ≤ n ≤ 100, each from 1 to 10⁵), each available without limit. Print the fewest coins that add up to exactly x (1 ≤ x ≤ 10⁵), or -1 if no combination does.
Input: n and x, then the n values. Output: one number.
| Input | Output |
|---|---|
3 111 5 7 | 3 |
4 tests · Ctrl+Enter
Hints
dp[s]is the fewest coins for the sums, orNoneifscan't be made. There is noINFto add 1 to.if let d = dp[s - v]uses only sums that can be made, anddp[s] min= d + 1keeps the better one;Noneloses to every value.- The -1 the statement asks for appears once, at the output.
- A table of
-1orINFtested with==is what the sentinel_table lint flags; Optionals shows?Intinstead.