3 ms·
I don't know if I would consider this easy for a beginner, but it does lead to an interesting result in the 'contrapositive' case: Any recursive solution can b
by zwkrt 7y ago
I don't know if I would consider this easy for a beginner, but it does lead to an interesting result in the 'contrapositive' case:
Any recursive solution can be turned into an iterative solution if you store the arguments of the recursive function in a data structure on the heap, and turn the recursive call itself into an access/modification of the data structure. As a specific example, any tail-recursive function can be turned into a for loop that modifies a stack. Recursive functions that are not tail-recursive (such as fibonacci) will require more complex data structures, depending on their internal recursive structure. (fibonacci can use an indexable list, for instance).
This is the heart of memoization and dynamic programming.