@jesimielpogi018 you need an O(n) solution, one that, when given a 100-element array, will do 100 iterations, and not 10000 (O(n^2)). So you can't bruteforce with nested for-loops.
First of all, I apologise for being late, but thank you.
If a function is memoized, from my understanding, it will help in its next call, because the previous results from the previous call has no reason to be deleted.
take a function fibonacci(n):
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-2) + fibonacci(n-1)
fibonacci(3) -> fibonacci(1) + fibonacci(2)
-> 1 + fibonacci(0) + fibonacci(1)
-> 1 + 0 + 1
-> 2
With memoization
MEMO = {0:0, 1:1}
def fibonacci(n):
if n in MEMO:
return MEMO[n]
MEMO[n] = ans = fibonacci(n-2) + fibonacci(n-1)
return ans
Now call fibonacci(3):
MEMO[3] = fibonacci(1) + fibonacci(2) = 2
v v
MEMO[1] = 1 MEMO[2] = MEMO[1] + MEMO[0] = 1
MEMO dictionary is updated each call, now it is
{0:0, 1:1, 2:1, 3:2}
Now call fibonacci(4):
MEMO[4] = fibonacci(2) + fibonacci(3) = 3
v v
1 2
|_______ _______|
V
saved in MEMO from last call, saving time by not re-calculating the results
updated MEMO dictionary:
{0:0, 1:1, 2:1, 3:2, 4:3}
Now call fibonacci(5):
MEMO[5] = fibonacci(3) + fibonacci(4) = 5
v v
2 3
Now, can you see why it works?
#TIL about the divmod operator, thank you!
I realy like this solution, is so much clean
The same thinking :D
i had thought about this
Alhamdulillah :)
This is a well known problem in Math, I suggest you read literature about it. It's a math heavy task.
@jesimielpogi018 you need an O(n) solution, one that, when given a 100-element array, will do 100 iterations, and not 10000 (O(n^2)). So you can't bruteforce with nested
for
-loops.fr, however take a look at this: https://www.codewars.com/kata/58db9545facc51e3db00000a
They hate yo ass for some reason
thanks i get it now
No probs :)
Thanks!!
nice solution . it reads like machine language .
First of all, I apologise for being late, but thank you.
If a function is memoized, from my understanding, it will help in its next call, because the previous results from the previous call has no reason to be deleted.
take a function
fibonacci(n)
:Hope this helps!
In my world the clock turns in the opposite diretion.
Loading more items...