What is memoization?

By vivek kumar in 20 Jul 2024 | 09:06 pm
vivek kumar

vivek kumar

Student
Posts: 552
Member since: 20 Jul 2024

What is memoization?

20 Jul 2024 | 09:06 pm
0 Likes
Prince

Prince

Student
Posts: 557
Member since: 20 Jul 2024

Memoization is a technique used to optimize algorithms by storing the results of expensive function calls and reusing these results when the same inputs occur again. This technique is especially useful in dynamic programming and recursive algorithms where the same computations are performed multiple times.



Consider the Fibonacci sequence, where each number is the sum of the two preceding numbers:



def fibonacci(n, memo={}):

    if n in memo:

        return memo[n]

    if n <= 1:

        return n

    memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)

    return memo[n]


20 Jul 2024 | 11:26 pm
0 Likes

Report

Please describe about the report short and clearly.