first thought
- sort the input array
- check from biggest to smallest
- pick current coin from most to none
solution
|
|
problem
miss the situation when amount is 0…
reason
modification
add this case
|
|
problem
wrong answer
reason
- actually the first problem has already reveal the problem.The base case in the recursion is totally wrong.It should be the one which we make the amount is 0.And there’s also no need to add a individual case.
- and there’s another stupid mistake:
count + maxNum
. It should becount + j
modification
change the base case into amount == 0
|
|
problem
TLE
modification
tried to skip some unnecessary loop, but it didn’t work.
|
|
this can be solved using DP. Update later.
DP solution
first thought
- similar with the 0-1 knapsack, build the 2d array to represent the count of two factors: current coin and sum.
- each point at the matrix represent the minimum count at this coin.
- iterate the current coin for k times, which k is the number of current coin. And save the minimum count for the current coin.
- iterate to the end, which is have chosen all the coins and the sum is amount.
solution
|
|
problem
TLE at the last case…
promote
The solution is right, but it can also make some pruning. There’re some situations that can be optimized.
- In initializing the matrix, there’s no necessary using n+1 rows.
- If the amount is smaller than the current coin. There’s no necessary to calculate.
modification
|
|