22. Coin change
You have unlimited coins of each value in coins. Write coinChange coins amount: some k when k is the smallest number of coins that add up to amount, and none when no combination does.
Always taking the largest coin that fits does not work: with coins [1, 3, 4], 6 is 3 + 3, not 4 + 1 + 1.
Examples
- Input
coinChange [1, 2, 5] 11Outputsome 3 - Input
coinChange [2] 3Outputnone - Input
coinChange [1] 0Outputsome 0
Submitting also runs 6 hidden tests.
Constraints
- Coin values are positive.
- Hidden tests use amounts up to 6,249, where plain recursion runs out of time.
Running and submitting solutions, the hints and the editorial come with the Academy Pass. The daily problem is open to every account.
Loading editor…
Type
\to in the editor, or click:Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.
ReadyLn 1, Col 1Lean 4
Draft saved in this browser