3 ms·
It's possible to do this using just a one-dimensional array. Since we only need the last column of the matrix we calculated to calculate the next column, the ar
by sur 15y ago
It's possible to do this using just a one-dimensional array. Since we only need the last column of the matrix we calculated to calculate the next column, the array can be updated in-place.
coins = [1, 2, 5, 10, 20, 50, 100, 200]
total = 200
matrix = [0] * (total + 1)
matrix[0] = 1
for coin in coins:
for j in range(coin, len(matrix)):
matrix[j] += matrix[j - coin]