https://leetcode.com/problems/n-th-tribonacci-number/description/
N-th Tribonacci Number - LeetCode
N-th Tribonacci Number - 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. Example 1: Input: n = 4 Output: 4 Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1
leetcode.com
1
2
3
4
5
6
7
8
9
10
11
|
class Solution:
def tribonacci(self, n: int) -> int:
t = [0, 1, 1]
while n+1>len(t):
t.append(t[-1]+t[-2]+t[-3])
if n<=2:
answer = t[n]
else:
answer = t[-1]
return answer
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
백준 27324 ゾロ目 (Same Numbers) (0) | 2023.01.31 |
---|---|
백준 27323 長方形 (Rectangle) (0) | 2023.01.31 |
leetcode 196. Delete Duplicate Emails (0) | 2023.01.29 |
leetcode 2433. Find The Original Array of Prefix Xor (0) | 2023.01.27 |
leetcode 2545. Sort the Students by Their Kth Score (0) | 2023.01.27 |
댓글