# Is the time complexity of this function O (n ^ 3)? And O (n) for your memorable solution?

Given this naive recursive function:

``````def the largest_tower (heights_and_widths, prev_height = 1000, prev_width = 1000):
if it is not alturas_y_widths:
return 0

results = {0}

for i in range (len (heights_and_widths)):
current_height, current_width = heights_and_widths[i]

if current_height <prev_height and current_width <prev_width:
subarray = heights_and_widths[:i] + alturas_y_widths[i+1:]
results.add (1 + larger_tower (subarray, current_height, current_width))

return max (results)
``````

At first glance, I would have said that the temporal complexity of this function is O (n ^ 2). But, if I'm not wrong, I would explain it with simple words would be: This function can call itself `(n-1) + (n-2) + ... + 1` times, being that `O (n ^ 2)`, but a total of n times, so the complexity of real time is `O (n ^ 3)`. I'm right

And for the memorized version:

``````def Click for mayor_tower_memoized (heights_and_widths, prev_height = 1000, prev_width = 1000, cache = None):
if it is not alturas_y_widths:
return 0

if the cache is None:
cache = {}

results = {0}

for i in range (len (heights_and_widths)):
current_height, current_width = heights_and_widths[i]

if current_height <prev_height and current_width <prev_width:
subarray = tupla (heights_and_widths[:i] + alturas_y_widths[i+1:])

if (current_height, current_width) is not cached:
cache[(current_height, current_width)] = mayor_tower_memoized (
subarray, current_height, current_width, cache
)
results.add (1 + cache[(current_height, current_width)])

return max (results)
``````

I find this more complicated to explain. My intuition tells me that it is `In)`, since the recursion tree for each entry is generated only once, but I'm not sure.