变个样,又不知道怎么解决了,动态规划确实不是那么好理解。根据上一题的经验,还是从递归入手,先实现递归,再琢磨动态规划的解决方案,就有点循序渐进的味道。正如 What is an easy way to understand the coin change problem in dynamic programming? 的一个回答所言:
Instead of thinking about filling a matrix, think in terms of the recurrence relation. I have seen a lot of people who try to think dynamic programming problems in terms of filling a table/array. But this approach is problematic. For example, if the state representation has more than two dimensions, then this approach does not help much.
The essence of dynamic programming is the idea of a state space and a recurrence relation between states. Once you figure that out for any problem, implementation is straightforward.
参考了这些,虽然还是没有完全理解,程序在leetcode上通过了,程序其实就短短的几行,The code has a O(NC) pseudo-polynomial complexity where N is the amount and C the number of input coins
。
- Number of ways to make change for an amount (coin change) 看了这个,终于明白了一点
- Dynamic programming - Ways of Coin Change
- Coin Change | DP-7
- Coin Change Problem Recursive Solution
- What is an easy way to understand the coin change problem in dynamic programming?
Java
1 | class Solution { |
Submission Detail
- 27 / 27 test cases passed.
- Runtime: 4 ms
- Your runtime beats 78.72 % of java submissions.
JavaScript
1 | /** |
Submission Detail
- 27 / 27 test cases passed.
- Runtime: 60 ms
- Your runtime beats 89.86 % of javascript submissions.