Dynamic Programming (Fibonacci Sequence)
Dynamic programming is a technique for solving problems by breaking them down into smaller subproblems and storing the solutions to those subproblems to avoid redundant work.
python
# Fibonacci sequence using dynamic programming
def fibonacci(n):
if n <= 1:
return n
memo = [0] * (n + 1)
memo[1] = 1
for i in range(2, n + 1):
memo[i] = memo[i - 1] + memo[i - 2]
return memo[n]
No comments:
Post a Comment