1137. N-th Tribonacci Number

package LeetCode_1137

/**
 * 1137. N-th Tribonacci Number
 * https://leetcode.com/problems/n-th-tribonacci-number/description/
 *
 * 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 + 2 = 4
 * */
class Solution {
    fun tribonacci(n: Int): Int {
        if (n<=1){
            return n
        }
        if (n==2){
            return 1
        }
        val dp = IntArray(n+1)
        dp[0] = 0
        dp[1] = 1
        dp[2] = 1
        for (i in 3 .. n){
            dp[i] = dp[i-3]+dp[i-2]+dp[i-1]
        }
        return dp[dp.size-1]
    }
}

原文地址:https://www.cnblogs.com/johnnyzhao/p/12635618.html

时间: 2024-08-01 11:36:35

1137. N-th Tribonacci Number的相关文章

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. 我写的code: Map<Integer, Integer> map = new HashMap<>(); public int tribonacci(int n) { if (n ==

LeetCode Weekly Contest 147

1137. 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

Project Euler 做题记录

Project Euler 太好玩了...(雾 Problem 675 设 \(\omega(n)\) 表示 \(n\) 的质因子个数,\(S(n)=\sum_{d|n}2^{\omega(d)}\),求 \(F(n)=\sum_{i=2}^nS(i!) \bmod (10^9+87)\). \(n=10^7\) solution \(n=\prod_{i=1}^kp_i^{e_i}\) \(S(n)=\prod_{i=1}^k(2e_i+1)\) 线性筛求出每个数的最小质因子之后就可以对 \(

02 - Exercise About Tribonacci Sequence

Description: Well met with Fibonacci bigger brother, AKA Tribonacci. As the name may already reveal, it works basically like a Fibonacci, but summing the last 3 (instead of 2) numbers of the sequence to generate the next. And, worse part of it, regre

1137 Final Grading (25 分)

1137 Final Grading (25 分) For a student taking the online course "Data Structures" on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from the online program

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit