Dynamic Programming
Fibonacci Sequence
大自然中,有許多事物包藏了斐波那契數(Fibonacci Numbers)的結構,比如:松果的排列、等角螺線、黃金分割、花瓣數目…等。在數學中,斐波那契數列(Fibonacci Sequence)是以遞迴的方式來定義:
F(n)=⎩⎪⎨⎪⎧01F(n−1)+F(n−2)n=0n=1n>1
Dynamic Programming
Question
首先來看一個題目:
❓ 數字表達方式
給定一個數字 N,有幾種方式可以將 N 表示為數字 1 和 2 的和?其中 1+2 和 2+1 應視為不同的方式。
Solution: Enumeration
當 N=1 時:
1=1
當 N=2 時:
22=1+1=2
當 N=3 時:
333=1+1+1=2+1=1+2
當 N=4 時:
44444=1+1+1+1=2+1+1=1+2+1=1+1+2=2+2
Solution: Dynamic Programming
dp[N]=Number of different ways to write the number N as the sum of 1 and 2=dp[N-1]+dp[N-2]
[Lecture] How to Spot Recurrence Relations?
[Lecture] 0/1 Knapsack Problem