파비의 매일매일 공부기록

2023.01.30 Today's Challenge 본문

Problem Solving/LeetCode

2023.01.30 Today's Challenge

fabichoi 2023. 1. 30. 23:45

https://leetcode.com/problems/n-th-tribonacci-number/

 

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

정말 간단한 피보나치 문제의 정말 소소한 변형

class Solution:
    def tribonacci(self, n: int) -> int:
        ar = [0, 1, 1, 2] + [0] * 40
        if n <= 3:
            return ar[n]
        for i in range(4, 38):
            ar[i] = ar[i-3] + ar[i-2] + ar[i-1]
        return ar[n]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.01 Today's Challenge  (0) 2023.02.01
2023.01.31 Today's Challenge  (0) 2023.01.31
2023.01.29 Today's Challenge  (0) 2023.01.29
2023.01.28 Today's Challenge  (0) 2023.01.28
2023.01.27 Today's Challenge  (0) 2023.01.27
Comments