1. Description
The Fibonacci numbers, commonly denoted F(n) form as sequence, called the Fibonacci sequence, such that each number is the sum of two preceding oens, starting 0 and 1. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
Given n, calculate F(n).
constraints :
- 0 <= n <= 30
2. Algorithms
- Start
- Declare variable named dp and initialize to [0, 1].
- Repeat through integers range 2 to n.
- Append the result of sum of two previous values.
- Return the final value.
- End
3. Codes
class Solution:
def fib(self, n: int) -> int:
dp = [0, 1]
for i in range(2, n+1) :
dp.append(dp[i-1] + dp[i-2])
return dp[n]
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
520 Detect Capital (0) | 2022.09.03 |
---|---|
511 Game Play Analysis I (0) | 2022.09.03 |
507 Perfect Number (0) | 2022.09.03 |
506 Relative Ranks (0) | 2022.09.03 |
504 Base 7 (0) | 2022.09.02 |