문제
The Tribonacci sequence Tn is defined as follows:
T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Given n, return the value of Tn.
T0 = 0, T1 = 1, T2 = 1, Tn+3 = Tn + Tn+1 + Tn+2 이고 n이 주어질 때,
Tn을 구하는 문제이다.
피보나치의 업그레이드 버젼인 트리보나치! 이다.
Example 1:
Input: n = 4
Output: 4
Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4
Example 2:
Input: n = 25
Output: 1389537
풀이
아이디어
리스트에 초기값 T0, T1, T2를 만들어 놓고 T3이후에는 Tn-3 + Tn-2 + Tn-1을 추가한다.
코드
class Solution:
def tribonacci(self, n: int) -> int:
tribonacci = [0, 1, 1]
for i in range(3, n+1):
tribonacci.append(sum(tribonacci[i-3:i]))
return tribonacci[n]
후기
이 문제 자체는 매우 쉽지만, 이걸 이용해서 어떤 어려운 문제들이 나올 지 기대된다.
'Algorithm' 카테고리의 다른 글
[LeetCode] 1289. Minimum Falling Path Sum II (Daily Question) (1) | 2024.04.26 |
---|---|
[LeetCode] 2370. Longest Ideal Subsequence (Daily Question) (0) | 2024.04.25 |
[LeetCode] 310. Minimum Height Trees (Daily Question) (0) | 2024.04.23 |
[LeetCode] 752. Open the Lock (Daily Question) (0) | 2024.04.22 |
[LeetCode] 1971. Find if Path Exists in Graph (Daily Question) (0) | 2024.04.22 |