Description

e 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

Example 2:

Input: n = 25
Output: 1389537

Constraints:

  • 0 <= n <= 37
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.

Analyse

类似于fibonacci,由两项的递推变成了三项,也就是tribonacci

T0 = 0

T1 = 1

T2 = 1

Tn+3 = Tn + Tn+1 + Tn+2

构造一个递推公式,直接用递归来做,写起来很简单,可是超时了,原因是很多数据重复计算了

int tribonacci(int n) {
if (n == 0 || n == 1) return n;
if (n == 2) return 1; return tribonacci(n-3) + tribonacci(n-2) + tribonacci(n-1);
}

改用动态规划的思想,AC了

int tribonacci(int n) {
int list[38] = {0, 1, 1,};
if (n < 3) return list[n]; for (int i = 3; i <= n; i++)
{
list[i] = list[i-3] + list[i-2] + list[i-1];
} return list[n];
}

但还是比LeetCode上其他代码要慢,看了下更快的代码,唯一的差别就是把数组换成了vector

int tribonacci(int n) {
vector<int> list(38);
int *list = (int*)malloc(sizeof(int) * 38); // 速度和数组一样,也比vector慢
list[0] = 0;
list[1] = 1;
list[2] = 1; for (int i = 3; i <= n; i++)
{
list[i] = list[i-3] + list[i-2] + list[i-1];
} return list[n];
}

[LeetCode] 1137. N-th Tribonacci Number的更多相关文章

  1. 【Leetcode_easy】1137. N-th Tribonacci Number

    problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...

  2. LeetCode.1137-第N个泰波那契数(N-th Tribonacci Number)

    这是小川的第409次更新,第441篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第260题(顺位题号是1137).Tribonacci(泰波那契)序列Tn定义如下: 对于n&g ...

  3. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  4. 【LeetCode】1137. N-th Tribonacci Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. 【leetcode】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 ...

  6. 1137. N-th Tribonacci Number(Memory Usage: 13.9 MB, less than 100.00% of Python3)

    其实思路很简单,套用一下普通斐波那契数列的非递归做法即可,不过这个成绩我一定要纪念一下,哈哈哈哈哈 代码在这儿: class Solution: def tribonacci(self, n: int ...

  7. LeetCode 287. Find the Duplicate Number (找到重复的数字)

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  8. LeetCode之“排序”:Largest Number

    题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...

  9. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

随机推荐

  1. CodeForces Round #499 Div2

    A: Stages 题意: 给你n个字符, 现在需要从中选取m个字符,每个字符的花费为在字母表的第几位,并且如果选了某个字符, 那么下一个选择的字符必须要在字母表的2位之后, 假如选了e 那么 不能选 ...

  2. VS2017 之 MYSQL实体数据模

    Photon Server 和 Unity3D 数据交互: Photon Server 服务端编程 Unity3D 客户端编程 VS2017 之 MYSQL实体数据模 一.新建数据库连接后,点击下一步 ...

  3. "ANDROID" 支持的KEYCODE

      例子: adb shell input text keyevent 4 0 -->  "KEYCODE_UNKNOWN" 1 -->  "KEYCODE_ ...

  4. ~!#$%^&*这些符号怎么读? 当然是用英语(键盘特殊符号小结)

    ~!#$%^&*这些符号怎么读? 当然是用英语(键盘特殊符号小结)   感谢原文作者:http://www.360doc.com/content/14/0105/20/85007_342874 ...

  5. 阿里巴巴资深技术专家雷卷:值得开发者关注的 Java 8 后时代的语言特性

    作者 | 阿里巴巴资深技术专家  雷卷,GitHub ID @linux-china 导读:在 Python.JavaScript 等一众编程语言崛起风靡之际,一代霸主 Java 风采虽不及当年,但仍 ...

  6. 题解 洛谷P2833 【等式】

    运用暴力解方程吸氧过了这道题 通过数据范围看,要是枚举x和y只能炸掉三成的数据. 所以考虑枚举从x1到x2枚举x,通过方程移项可知y=-(ax+c)/b,再判断y是否在y1和y2之间即可. 本题本做法 ...

  7. 024 实例5-身体质量指数BMI

    目录 一."身体质量指数BMI"问题分析 1.1 身体质量指数BMI 1.2 问题需求 二."身体质量指数BMI"实例讲解 2.1 身体质量指标BMI 2.1. ...

  8. springboot使用memcache缓存

    Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  9. Python的6种运算符(日记)

    学习了许久的Python,我单独总结出了Python中比较常见的6种运算符,感觉略有不全,希望大伙可以一起讨论与研究Python! 一.算术运算符 加 减 - 乘 * 除 / 取余 % 取整 // 异 ...

  10. [大数据学习研究]2.利用VirtualBox模拟Linux集群

    1. 在主机Macbook上设置HOST 前文书已经把虚拟机的静态IP地址设置好,以后可以通过ip地址登录了.不过为了方便,还是设置一下,首先在Mac下修改hosts文件,这样在ssh时就不用输入ip ...