Exercise 1.14 count change的时间复杂度

题目

  Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases? 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

设amount是N。

由于count-change算法是二叉树分支递归,所以树的高度决定了时间复杂度。二叉树的最大高度是 N/1,最小高度是N/50,因此时间复杂度介于 2^N 与2^(N/50)之间,即O(2^N)。这其中进行了大量的重复计算。

如果使用HashMap将 "(amount, change_list)"=>"结果" 中间计算结果存储起来,则时间复杂度取决于(amount,change_list)有多少种组合数量。change_list有包含50、25、10、5、1,共有C(5,0)+C(5,1)+C(5,2)+C(5,3)+C(5,4)+C(5,5)种组合,amount则有N种取值。因此时间复杂度是O(N)。

原文地址:https://www.cnblogs.com/linghuaichong/p/3974432.html