Skip to content

Commit 317ed34

Browse files
authored
Create 1137. N-th Tribonacci Number.java
1 parent 5fe998f commit 317ed34

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// O(3n)
2+
3+
class Solution {
4+
public int tribonacci(int n) {
5+
if (n <= 1) return n;
6+
7+
int[] num = new int[n+1];
8+
num[1] = 1; num[2] = 1;
9+
if (n == 2) return num[2];
10+
11+
for (int i = 3; i<=n; i++) {
12+
for (int k = i-3; k<i; k++) num[i] += num[k];
13+
}
14+
return num[n];
15+
}
16+
}

0 commit comments

Comments
 (0)