본문 바로가기

Leetcode

Leetcode 1137. N-th Tribonacci Number

 

오늘 오전의 리트코드!

n-th tribonacci 수를 구하는 것이다.

 

간단하지만 시간 초과를 고려하여 for문을 이용하였다.

처음에 recursive하게 코드를 구현했는데 시간 초과 난다고 퇴짜 맞았기 때문이다...

class Solution:
    def tribonacci(self, n: int) -> int:
        if n == 0:
            return 0
        t0 = 0
        t1 = 1
        t2 = 1
        tn = 0
        for i in range(n-2):
            tn = t0 + t1 + t2
            t0 = t1
            t1 = t2
            t2 = tn
            
        return t2

 

 

끝!

 

'Leetcode' 카테고리의 다른 글

Leetcode 563. Binary Tree Tilt  (0) 2021.12.09
Leetcode 268. Missing Number  (0) 2021.11.23
Leetcode 20. Valid Parentheses  (0) 2021.11.15
Leetcode 122. Best Time to Buy and Sell Stock II  (0) 2021.11.10
Leetcode 118. Pascal's Triangle  (0) 2021.11.06