被这题卡住了,因为动态规划好像不好理解,看《算法图解》第九章,说动态规划都从一个网格开始,需要具备 Overlapping Sub-problems
与 Optimal Substructure
。上网找线索与提示,最后发现先用递归方式实现,再看动态规划的方案就好理解些。主要参考了这几个:
- Find minimum number of coins that make a given value
- Introduction To Dynamic Programming – Fibonacci Series
- Dynamic Programming – Minimum Coin Change Problem
- What is an easy way to understand the coin change problem in dynamic programming?
- Change Problem
- Find minimum number of coins (using Dynamic Programming) | GeeksforGeeks
原来 Change-making problem 是 Knapsack problem 的一类,背包问题还属于NP问题呢。
此题递归方式的实现是指数时间复杂度,跟动态规划的实现相比差距确实极其明显。递归方式的解法完全不可用,输入[1,2,5],100则报Time Limit Exceeded。
Java
1 | class Solution { |
Submission Detail
- 182 / 182 test cases passed.
- Runtime: 37 ms
- Your runtime beats 28.19 % of java submissions.
JavaScript
1 | /** |
Submission Detail
- 182 / 182 test cases passed.
- Runtime: 80 ms
- Your runtime beats 95.69 % of javascript submissions.