SeangG
def _(): _()
SeangG
전체 방문자
오늘
어제
  • 분류 전체보기 (37)
    • Programming Language (0)
      • Python (0)
      • Web (0)
    • Algorithm (34)
    • Art (0)
      • 3D Modeling (0)
      • Pixel (0)
      • Picture (0)
    • Game (0)
    • Project (3)
      • Problems (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • dfs
  • graph
  • Python
  • leetcode
  • Leecode
  • LeedCode
  • 문자열
  • Tree
  • github oauth
  • react.js
  • 로마 숫자
  • Queue
  • 매핑 테이블
  • spring boot
  • WSL
  • string
  • Fast Refresh
  • BFS
  • Daily Question
  • next.js

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
SeangG

def _(): _()

Algorithm

[LeetCode] 1137. N-th Tribonacci Number (Daily Question)

2024. 4. 24. 10:13

문제


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
    'Algorithm' 카테고리의 다른 글
    • [LeetCode] 1289. Minimum Falling Path Sum II (Daily Question)
    • [LeetCode] 2370. Longest Ideal Subsequence (Daily Question)
    • [LeetCode] 310. Minimum Height Trees (Daily Question)
    • [LeetCode] 752. Open the Lock (Daily Question)
    SeangG
    SeangG

    티스토리툴바