零钱找零:重叠的子问题是什么?

时间:2018-08-19 17:54:38

标签: dynamic-programming

This is the source of the problem at uva.onlinejudge.org

问题基本上是这样的:
鉴于必须要给N钱!我们需要找出我们可以给多少个最小硬币以及这些硬币的总价值,以使使用n个给定面额的给定额外金额最小!

例如:

public string typedChars = string.Empty;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    string CTRL = (e.Control ? "CTRL + " : "");
    string ALT = (e.Alt ? "ALT + " : "");
    string SHIFT = (e.Shift ? "Shift + " : "");
    string KEYPRESSED = e.KeyCode.ToString();
    typedChars = $"{CTRL}{ALT}{SHIFT}{KEYPRESSED}"; // Puts all pressed key combination to a string for the HotKey combination

    for (int i = 0; i < (dataGridView1.Rows.Count); i++) //counting all the rows
    {
        if (dataGridView1.Rows[i].Cells[3].Value.ToString().Equals(typedChars)) // search for an exact value in cell 3 and find that row (HotKey)
        {
            String Path = dataGridView1.Rows[i].Cells[2].Value.ToString(); //use from that founded row the cell 2 value (Path)
            //do something here with the value
        }
    }
}

我的问题是:
这里有哪些重叠的子问题?

我的意思是:
有重叠的子问题吗?
因为我找不到任何东西...

1 个答案:

答案 0 :(得分:0)

重叠的子问题是总和达到特定美分的硬币/票据的最小数量。

for coin_value in coins(sorted)
   for sum where num[sum] is valid
      num[ sum + coin_value ] = Min( num[sum + coin_value], num[sum] + 1 )

请参阅:Dynamic Programming Coin Change Limited Coins

对该问题的约束足够低,您可以使用该问题的公认答案。唯一的不同是,您计算出总的硬币/纸币的最小数量,以得出目标价格,然后继续超过目标价格。当您完成阵列的填写后,请从目标价格开始,一直上涨直到找到有效答案为止。

对硬币/钞票进行排序,并从最大的面额开始向下移动。

(我的解决方案已在UVa上接受。)