python-algorithm
leetcode 1137. N-th Tribonacci Number
무적김두칠
2023. 1. 30. 12:36
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 |
반응형