Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cffdb17

Browse files
Merge pull request #327 from cheehwatang/add-1137-N-thTribonacciNumber
Add 1137. N-th Tribonacci Number (Recursive)
2 parents 82808e5 + 42aec1e commit cffdb17

File tree

2 files changed

+78
-19
lines changed

2 files changed

+78
-19
lines changed

‎README.md‎

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 6th</td>
32+
<td>1137. <a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
33+
<td align="center">$\text{\color{TealBlue}Easy}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Recursive.java">Dynamic Programming - Memoization</a>
36+
</td>
37+
<td align="center">
38+
<a href="#dynamic-programming">Dynamic Programming</a>,
39+
<a href="#math">Math</a>,
40+
<a href="#memoization">Memoization</a>
41+
</td>
42+
</tr>
3043
<tr>
3144
<td align="center">September 5th</td>
3245
<td>1137. <a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
@@ -76,19 +89,6 @@
7689
<a href="#counting">Counting</a>
7790
</td>
7891
</tr>
79-
<tr>
80-
<td align="center">September 1st</td>
81-
<td>16. <a href="https://leetcode.com/problems/3sum-closest/">3Sum Closest</a></td>
82-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
83-
<td align="center">
84-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/16.%203Sum%20Closest/ThreeSumClosest.java">Sorting & Two Pointers</a>
85-
</td>
86-
<td align="center">
87-
<a href="#array">Array</a>,
88-
<a href="#sorting">Sorting</a>,
89-
<a href="#two-pointers">Two Pointers</a>
90-
</td>
91-
</tr>
9292
</table>
9393
</br>
9494
<hr>
@@ -4329,13 +4329,15 @@
43294329
<tr>
43304330
<td align="center">1137</td>
43314331
<td><a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
4332-
<td align="center">
4333-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Iterative.java">Java</a>
4332+
<td align="center">Java with Dynamic Programming
4333+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Iterative.java">Iteratively</a> or
4334+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Recursive.java">Recursively (Memoization)</a>
43344335
</td>
43354336
<td align="center">$\text{\color{TealBlue}Easy}$</td>
43364337
<td align="center">
43374338
<a href="#dynamic-programming">Dynamic Programming</a>,
4338-
<a href="#math">Math</a>
4339+
<a href="#math">Math</a>,
4340+
<a href="#memoization">Memoization</a>
43394341
</td>
43404342
<td></td>
43414343
</tr>
@@ -6273,13 +6275,15 @@
62736275
<tr>
62746276
<td align="center">1137</td>
62756277
<td><a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
6276-
<td align="center">
6277-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Iterative.java">Java</a>
6278+
<td align="center">Java with Dynamic Programming
6279+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Iterative.java">Iteratively</a> or
6280+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Recursive.java">Recursively (Memoization)</a>
62786281
</td>
62796282
<td align="center">$\text{\color{TealBlue}Easy}$</td>
62806283
<td align="center">
62816284
<a href="#dynamic-programming">Dynamic Programming</a>,
6282-
<a href="#math">Math</a>
6285+
<a href="#math">Math</a>,
6286+
<a href="#memoization">Memoization</a>
62836287
</td>
62846288
<td></td>
62856289
</tr>
@@ -6726,6 +6730,22 @@
67266730
</td>
67276731
<td></td>
67286732
</tr>
6733+
<tr>
6734+
<td align="center">1137</td>
6735+
<td><a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
6736+
<td align="center">
6737+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Recursive.java">Java</a>
6738+
</td>
6739+
<td align="center">$\text{\color{TealBlue}Easy}$</td>
6740+
<td align="center">
6741+
<a href="#dynamic-programming">Dynamic Programming</a>,
6742+
<a href="#math">Math</a>,
6743+
<a href="#memoization">Memoization</a>
6744+
</td>
6745+
<td>Solution Using
6746+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Iterative.java"><em>Dynamic Programming (Iterative)</em></a>
6747+
</td>
6748+
</tr>
67296749
</table>
67306750
<p align="right"><a href="#topics"> :arrow_up:<b>Back To Top</b></a></p>
67316751
<hr>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(n),
4+
// where 'n' is the input 'n'.
5+
// The recursive call stack has a maximum height of 'n'.
6+
//
7+
// Space Complexity : O(n),
8+
// where 'n' is the input 'n'.
9+
// We use a memo of size 'n' to store the tribonacci numbers.
10+
// Additionally, the recursive call stack has a maximum height of 'n'.
11+
12+
public class NthTribonacciNumber_Recursive {
13+
14+
// Approach:
15+
// We use the recursive method, using the 'memo' keep track of the calculated number to lower the time complexity.
16+
17+
public int tribonacci(int n) {
18+
19+
// Use an integer array as 'memo' to record the results that was calculated.
20+
// This is so to not repeat the same calculation over the recursive calls.
21+
int[] memo = new int[n + 1];
22+
return tribonacci(n, memo);
23+
}
24+
25+
private int tribonacci(int n, int[] memo) {
26+
if (n == 0 || n == 1) return n;
27+
if (n == 2) return 1;
28+
29+
// Record the first three element in the Fibonacci sequence.
30+
memo[1] = 1;
31+
memo[2] = 1;
32+
33+
// If the ways for n is not recorded in the 'memo' yet, then calculate and record the result into the memo.
34+
if (memo[n] == 0) {
35+
memo[n] = tribonacci(n - 1, memo) + tribonacci(n - 2, memo) + tribonacci(n - 3, memo);
36+
}
37+
return memo[n];
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /