Skip to content

Commit 3a07670

Browse files
authored
Update N-th Tribonacci Number.java
1 parent da0b398 commit 3a07670

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Easy/N-th Tribonacci Number.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
class Solution {
22
public int tribonacci(int n) {
3-
int[] dp = new int[Math.max(n + 1, 3)];
4-
dp[0] = 0;
5-
dp[1] = 1;
6-
dp[2] = 1;
7-
return helper(n, dp);
8-
}
9-
10-
private int helper(int n, int[] dp) {
11-
if (n == 0 || dp[n] != 0) {
12-
return dp[n];
3+
int[] arr = new int[38];
4+
arr[1] = arr[2] = 1;
5+
for (int i = 3; i < 38; i++) {
6+
arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3];
137
}
14-
dp[n] = helper(n - 1, dp) + helper(n - 2, dp) + helper(n - 3, dp);
15-
return dp[n];
8+
return arr[n];
169
}
1710
}

0 commit comments

Comments
 (0)