値Nを与えた場合、Nセントに変更したい場合、S = {S1、S2、..、Sm}値のコインのそれぞれを無限に供給すると、私たちは変更を加えることができますか?コインの順序は関係ありません。例えば、N = 4およびS = {1,2,3}の場合、{1,1,1,1}、{1,1,2}、{2,2}の4つの解が存在する。 、{1,3}。出力は4でなければなりません。N = 10、S = {2、5、3、6}の場合、{2,2,2,2,2}、{2,2,3,3}、 {2,2,6}、{2,3,5}、{5,5}である。出力は5になるはずです。コインの交換のための空間最適化ソリューション
私は3つのアプローチを見つけましたHERE。しかし、空間的に最適化された動的プログラミングアプローチを理解することはできません。ここでは、1次元の配列表[]のみが使用されています。
int count(int S[], int m, int n)
{
// table[i] will be storing the number of solutions for
// value i. We need n+1 rows as the table is consturcted
// in bottom up manner using the base case (n = 0)
int table[n+1];
// Initialize all table values as 0
memset(table, 0, sizeof(table));
// Base case (If given value is 0)
table[0] = 1;
// Pick all coins one by one and update the table[] values
// after the index greater than or equal to the value of the
// picked coin
for(int i=0; i<m; i++)
for(int j=S[i]; j<=n; j++)
table[j] += table[j-S[i]];
return table[n];
}
これでソートされたS []が必要ですか?そうでない場合は、{3,2,6,5,4} – Walt
のようなSのためにどのように動作しますか?ソートされたS []は必要ありません。アルゴリズムの最後に、table []は{3,2,6,5,4}と{2,3,4,5,6}の値が同じでなければなりません。 – sunilnmu
あなたは{6,5,2,4}以上の乾いたランで私を助けてもらえますか?混乱しています:( – Walt