首页 > 其他 > 详细

Exercise 1.14 count change的时间复杂度

时间:2014-09-16 12:10:50      阅读:374      评论:0      收藏:0      [点我收藏+]

题目

  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)。

Exercise 1.14 count change的时间复杂度

原文:http://www.cnblogs.com/linghuaichong/p/3974432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!