{"id":5360,"date":"2019-07-27T21:40:09","date_gmt":"2019-07-28T04:40:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5360"},"modified":"2019-07-27T21:43:03","modified_gmt":"2019-07-28T04:43:03","slug":"leetcode-1137-n-th-tribonacci-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1137-n-th-tribonacci-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1137. N-th Tribonacci Number"},"content":{"rendered":"\n<p>The Tribonacci sequence T<sub>n<\/sub>&nbsp;is defined as follows:&nbsp;<\/p>\n\n\n\n<p>T<sub>0<\/sub>&nbsp;= 0, T<sub>1<\/sub>&nbsp;= 1, T<sub>2<\/sub>&nbsp;= 1, and T<sub>n+3<\/sub>&nbsp;= T<sub>n<\/sub>&nbsp;+ T<sub>n+1<\/sub>&nbsp;+ T<sub>n+2<\/sub>&nbsp;for n &gt;= 0.<\/p>\n\n\n\n<p>Given&nbsp;<code>n<\/code>, return the value of T<sub>n<\/sub>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>\nT_3 = 0 + 1 + 1 = 2\nT_4 = 1 + 1 + 2 = 4\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 25\n<strong>Output:<\/strong> 1389537\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= n &lt;= 37<\/code><\/li><li>The answer is guaranteed to fit within a 32-bit integer, ie.&nbsp;<code>answer &lt;= 2^31 - 1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int tribonacci(int n) {\n    if (n == 0) return n;\n    int t0 = 0;\n    int t1 = 1;\n    int t2 = 1;\n    int t = 1;\n    for (int i = 3; i <= n; ++i) {\n      t = t0 + t1 + t2;\n      t0 = t1;\n      t1 = t2;\n      t2 = t;\n    }\n    return t;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Tribonacci sequence Tn&nbsp;is defined as follows:&nbsp; T0&nbsp;= 0, T1&nbsp;= 1, T2&nbsp;= 1, and Tn+3&nbsp;= Tn&nbsp;+ Tn+1&nbsp;+ Tn+2&nbsp;for n &gt;= 0. Given&nbsp;n, return the value&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,222,54],"class_list":["post-5360","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-easy","tag-iterative","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5360"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5360\/revisions"}],"predecessor-version":[{"id":5363,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5360\/revisions\/5363"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}