Efficiency python2 recursive and iterative

Recently I discovered a site called 51nod programming, just so he also learned python2 hand Python2 language and thought, by the way also review the algorithmic problems.

During the 1002 we made a number of tower problems, leads me to question the efficiency of recursive and iterative, this topic is

 

 

 

 

After reading the title I used a recursive algorithm, recursive top-down, bottom-up calculations, my code and results are as follows:

Algorithm is correct, but when large volumes of data, tle will happen, then I think the algorithm is not my problem, is not there a better way to calculate, so I checked the solution of this question and found that Solution of questions (dynamic programming) is oriented in a cyclic manner from a maximum value to obtain each layer, when the top layer is a maximum. This is my idea seems to be the same. Only one is a recursive iteration. So I think it should be efficiency

So Baidu a moment suddenly see the light, in theory, recursion and iteration time efficiency is the same, but in the actual process of recursive function calls need to continue to increase stack space, which results in memory consumption in this period will be larger than the cycle, it will be more time-efficient than iteration, iteration after all, just a cumulative process it, so I changed a bit my way on this question of ac

 

Guess you like

Origin blog.csdn.net/mxy88888/article/details/73845117