作者Rushia (早瀬ユウカの体操服 )
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Wed Apr 24 09:15:53 2024
https://leetcode.com/problems/n-th-tribonacci-number/description
1137. N-th Tribonacci Number
給你一個數字n,求出第 n 個 Tribonacci 數列是多少。
思路:
1.動態規劃,然後把空間壓一壓。
-----------------------------------------
class Solution:
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
if n <= 2:
return 1
n1, n2, n3 = 0, 1, 1
for i in range(3, n + 1):
n1, n2, n3 = n2, n3, n1 + n2 + n3
return n3
-----------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.138.10.4 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1713921355.A.562.html
→ JIWP: 大師 04/24 09:16
→ JIWP: 怎麼是DP 我兔了 04/24 09:16
推 DJYOSHITAKA: 大濕 04/24 09:20
推 SecondRun: 哇 是EZ 我要寫 04/24 09:24
→ digua: 大師 04/24 10:32