codekofi
← All problems

Problem 129

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1int solution(const vector<int>& coins, int amount) {
2
3 vector<int> fewest(amount + 1, INT_MAX);
4 fewest[0] = 0;
5
6 for (int value = 1; value <= amount; value++) {
7 for (int coin : coins) {
8
9 if (coin > value) {
10 continue;
11 }
12
13 if (fewest[value - coin] == INT_MAX) {
14 continue;
15 }
16
17 int used = fewest[value - coin] + 1;
18 fewest[value] = min(fewest[value], used);
19 }
20 }
21
22 if (fewest[amount] == INT_MAX) {
23 return -1;
24 }
25
26 return fewest[amount];
27}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.